Skip to content

Commit 2fb18e2

Browse files
committed
enumset without the unuseful set implementation
1 parent e528996 commit 2fb18e2

File tree

4 files changed

+67
-23
lines changed

4 files changed

+67
-23
lines changed

src/main/java/com/cedarsoftware/util/io/JsonWriter.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ public void writeImpl(Object obj, boolean showType, boolean allowRef, boolean al
10911091
}
10921092
else if (obj instanceof EnumSet)
10931093
{
1094-
writeEnumSet((EnumSet)obj, showType);
1094+
writeEnumSet((EnumSet)obj);
10951095
}
10961096
else if (obj instanceof Collection)
10971097
{
@@ -2211,7 +2211,7 @@ else if (neverShowType && MetaUtils.isPrimitive(o.getClass()))
22112211
}
22122212
}
22132213

2214-
public void writeEnumSet(final EnumSet<?> enumSet, boolean showType) throws IOException
2214+
public void writeEnumSet(final EnumSet<?> enumSet) throws IOException
22152215
{
22162216
out.write('{');
22172217
tabIn();
@@ -2224,24 +2224,44 @@ public void writeEnumSet(final EnumSet<?> enumSet, boolean showType) throws IOEx
22242224
newLine();
22252225
}
22262226

2227-
if (showType) {
2228-
writeType(enumSet, out);
2229-
out.write(",");
2230-
newLine();
2231-
}
2232-
22332227
writeJsonUtf8String("@enum", out);
22342228
out.write(':');
22352229

2230+
Enum<? extends Enum<?>> ee = null;
2231+
if (!enumSet.isEmpty())
2232+
{
2233+
ee = enumSet.iterator().next();
2234+
}
2235+
else
2236+
{
2237+
EnumSet<? extends Enum<?>> complement = EnumSet.complementOf(enumSet);
2238+
if (!complement.isEmpty())
2239+
{
2240+
ee = complement.iterator().next();
2241+
}
2242+
}
2243+
22362244
Field elementTypeField = MetaUtils.getField(EnumSet.class, "elementType");
2237-
Class elementType = (Class) getValueByReflect(enumSet, elementTypeField);
2245+
Class<?> elementType = (Class<?>) getValueByReflect(enumSet, elementTypeField);
2246+
if ( elementType != null)
2247+
{
2248+
// nice we got the right to sneak into
2249+
}
2250+
else if (ee == null)
2251+
{
2252+
elementType = MetaUtils.Dumpty.class;
2253+
}
2254+
else
2255+
{
2256+
elementType = ee.getClass();
2257+
}
22382258
writeJsonUtf8String(elementType.getName(), out);
22392259

2240-
Map<String, Field> mapOfFileds = MetaUtils.getDeepDeclaredFields(elementType);
2241-
//Field[] enumFields = elementType.getDeclaredFields();
2242-
int enumFieldsCount = mapOfFileds.size();
2243-
22442260
if (!enumSet.isEmpty()) {
2261+
Map<String, Field> mapOfFields = MetaUtils.getDeepDeclaredFields(elementType);
2262+
//Field[] enumFields = elementType.getDeclaredFields();
2263+
int enumFieldsCount = mapOfFields.size();
2264+
22452265
out.write(",");
22462266
newLine();
22472267

@@ -2272,7 +2292,7 @@ public void writeEnumSet(final EnumSet<?> enumSet, boolean showType) throws IOEx
22722292
{
22732293
boolean firstInEntry = true;
22742294
out.write('{');
2275-
for (Field f : mapOfFileds.values())
2295+
for (Field f : mapOfFields.values())
22762296
{
22772297
firstInEntry = writeField(e, firstInEntry, f.getName(), f, false);
22782298
}

src/main/java/com/cedarsoftware/util/io/MetaUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
*/
3737
public class MetaUtils
3838
{
39+
public enum Dumpty {}
40+
3941
private MetaUtils () {}
4042
private static final Map<Class, Map<String, Field>> classMetaCache = new ConcurrentHashMap<>();
4143
private static final Set<Class> prims = new HashSet<>();

src/main/java/com/cedarsoftware/util/io/Resolver.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,21 @@ protected Object createJavaObjectInstance(Class clazz, JsonObject jsonObj)
313313
String type = jsonObj.type;
314314

315315
// We can't set values to an Object, so well try to use the contained type instead
316-
if ("java.lang.Object".equals(type))
316+
if ("java.lang.Object".equals(type))
317317
{
318-
Object value = jsonObj.get("value");
319-
if (jsonObj.keySet().size() == 1 && value != null)
318+
Object value = jsonObj.get("value");
319+
if (jsonObj.keySet().size() == 1 && value != null)
320320
{
321-
type = value.getClass().getName();
322-
}
321+
type = value.getClass().getName();
322+
}
323+
}
324+
if (type == null)
325+
{
326+
Object mayEnumSpecial = jsonObj.get("@enum");
327+
if (mayEnumSpecial instanceof String)
328+
{
329+
type = "java.util.EnumSet";
330+
}
323331
}
324332

325333
Object mate;
@@ -571,12 +579,24 @@ private Object getEnumSet(Class c, JsonObject<String, Object> jsonObj)
571579
protected EnumSet<?> extractEnumSet(Class c, JsonObject<String, Object> jsonObj)
572580
{
573581
String enumClassName = (String) jsonObj.get("@enum");
574-
Class enumClass = MetaUtils.classForName(enumClassName, reader.getClassLoader());
582+
Class enumClass = enumClassName == null ? null
583+
: MetaUtils.classForName(enumClassName, reader.getClassLoader());
575584
Object[] items = jsonObj.getArray();
576585
if (items == null || items.length == 0)
577586
{
578-
return EnumSet.noneOf(enumClass);
587+
if (enumClass != null)
588+
{
589+
return EnumSet.noneOf(enumClass);
590+
}
591+
else
592+
{
593+
return EnumSet.noneOf(MetaUtils.Dumpty.class);
594+
}
579595
}
596+
else if (enumClass == null)
597+
{
598+
throw new JsonIoException("Could not figure out Enum of the not empty set " + jsonObj);
599+
}
580600

581601
EnumSet enumSet = null;
582602
for (Object item : items)

src/test/java/com/cedarsoftware/util/io/TestEmptyEnumSetOnJDK17.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ static class MultiVersioned
3333
{
3434
EnumSet<TestEnum> versions;
3535
String dummy;
36+
EnumSet<Thread.State> states;
3637

37-
public MultiVersioned(EnumSet<TestEnum> versions, String dummy) {
38+
public MultiVersioned(EnumSet<TestEnum> versions, String dummy, EnumSet<Thread.State> states) {
3839
this.versions = versions;
3940
this.dummy = dummy;
41+
this.states = states;
4042
}
4143

4244
public boolean equals(Object o) {
@@ -76,7 +78,7 @@ public void testEnumSetOnJDK17()
7678

7779
public void testEnumSetInPoJoOnJDK17()
7880
{
79-
MultiVersioned m = new MultiVersioned(EnumSet.of(TestEnum.V1, TestEnum.V3), "what");
81+
MultiVersioned m = new MultiVersioned(EnumSet.of(TestEnum.V1, TestEnum.V3), "what", EnumSet.of(Thread.State.NEW));
8082

8183
String json = JsonWriter.objectToJson(m);
8284
MultiVersioned target = (MultiVersioned) JsonReader.jsonToJava(json);

0 commit comments

Comments
 (0)