Last active
September 23, 2022 16:40
-
-
Save brimworks/7e544c40f081a67e32add086822f0a36 to your computer and use it in GitHub Desktop.
JsonValue avoiding casting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static JsonUtil.match; | |
public class Example { | |
class Pojo { | |
String field1; | |
int field2; | |
} | |
public Pojo toPojo(JsonValue val) { | |
return match(val, new JsonMatch<Pojo>() { | |
public Pojo matchDefault() { | |
return null; | |
} | |
public Pojo match(JsonObject obj) { | |
Pojo pojo = new Pojo(); | |
pojo.field1 = match(obj.get("field1"), new JsonMatch<String>() { | |
public String matchDefault() { | |
return null; | |
} | |
public String match(String str) { | |
return str; | |
} | |
}); | |
pojo.field2 = match(obj.get("field2"), new JsonMatchInt() { | |
public int matchDefault() { | |
return 0; | |
} | |
public int match(JsonNumber num) { | |
return num.intValue(); | |
} | |
}); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface JsonMatch<T> { | |
default T match(String str) { | |
return matchDefault(); | |
} | |
default T match(JsonObject obj) { | |
return matchDefault(); | |
} | |
default T match(JsonArray arr) { | |
return matchDefault(); | |
} | |
default T match(JsonNumber num) { | |
return matchDefault(); | |
} | |
default T match(boolean bool) { | |
return matchDefault(); | |
} | |
default T matchNull() { | |
return matchDefault(); | |
} | |
T matchDefault(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface JsonMatchInt { | |
default int match(String str) { | |
return matchDefault(); | |
} | |
default int match(JsonObject obj) { | |
return matchDefault(); | |
} | |
default int match(JsonArray arr) { | |
return matchDefault(); | |
} | |
default int match(JsonNumber num) { | |
return matchDefault(); | |
} | |
default int match(boolean bool) { | |
return matchDefault(); | |
} | |
default int matchNull() { | |
return matchDefault(); | |
} | |
int matchDefault(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonUtils { | |
public <T> T match(JsonValue value, JsonMatch<T> switcher) { | |
if (null == value || JsonValue.NULL == value) { | |
return switcher.matchNull(); | |
} else if (JsonValue.TRUE == value) { | |
return switcher.match(true); | |
} else if (JsonValue.FALSE == value) { | |
return switcher.match(false); | |
} else if (value instanceof JsonObject) { | |
return switcher.match((JsonObject)value); | |
} else if (value instanceof JsonArray) { | |
return switcher.match((JsonArray)value); | |
} else if (value instanceof JsonNumber) { | |
return switcher.match((JsonNumber)value); | |
} else if (value instanceof JsonString) { | |
return switcher.match((JsonString)value); | |
} | |
throw new IllegalStateException("Unexpected JsonValue subtype " + value.getClass()); | |
} | |
// TODO: Add "primitive" versions for ALL types to avoid auto/boxing and unboxing. | |
public int match(JsonValue value, JsonMatchInt switcher) { | |
if (null == value || JsonValue.NULL == value) { | |
return switcher.matchNull(); | |
} else if (JsonValue.TRUE == value) { | |
return switcher.match(true); | |
} else if (JsonValue.FALSE == value) { | |
return switcher.match(false); | |
} else if (value instanceof JsonObject) { | |
return switcher.match((JsonObject)value); | |
} else if (value instanceof JsonArray) { | |
return switcher.match((JsonArray)value); | |
} else if (value instanceof JsonNumber) { | |
return switcher.match((JsonNumber)value); | |
} else if (value instanceof JsonString) { | |
return switcher.match((JsonString)value); | |
} | |
throw new IllegalStateException("Unexpected JsonValue subtype " + value.getClass()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment