Skip to content

Instantly share code, notes, and snippets.

@SCP002
Last active April 11, 2021 03:47
Show Gist options
  • Save SCP002/5168bcc90c0d118fd1b06394f97b7c11 to your computer and use it in GitHub Desktop.
Save SCP002/5168bcc90c0d118fd1b06394f97b7c11 to your computer and use it in GitHub Desktop.
Java 13 (optionally 8): Decode & Encode partially known / dynamic JSON into classes. Require Jackson.
/*
* Java 13+ (Uses multi-line string).
* Java 8+ without multi-line string (line 87).
* Require Jackson dependency (Maven example; Add to <dependencies> in your pom.xml):
* <dependency>
* <groupId>com.fasterxml.jackson.core</groupId>
* <artifactId>jackson-databind</artifactId>
* <version>2.12.2</version>
* </dependency>
*/
package org.example;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"nestedKnownKey"})
class NestedKnownArray {
@JsonProperty("nestedKnownKey")
public Integer nestedKnownKey;
@JsonIgnore
public Integer excludedKnownKey; // Field sample for internal needs, excluded from JSON processing.
@JsonIgnore
private final Map<String, Object> unknown = new HashMap<>(); // All unknown fields go here.
@SuppressWarnings("unused")
@JsonAnyGetter
public Map<String, Object> getUnknown() { // Keep this method for Jackson.
return this.unknown;
}
@SuppressWarnings("unused")
@JsonAnySetter
public void setUnknown(String name, Object value) { // Keep this method for Jackson.
this.unknown.put(name, value);
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"topKnownKey", "nestedKnownArray"})
class Top {
@JsonProperty("topKnownKey")
public Integer topKnownKey;
@JsonProperty("nestedKnownArray")
public List<NestedKnownArray> nestedKnownArray = null;
@JsonIgnore
public Integer excludedKnownKey; // Field sample for internal needs, excluded from JSON processing.
@JsonIgnore
private final Map<String, Object> unknown = new HashMap<>(); // All unknown fields go here.
@SuppressWarnings("unused")
@JsonAnyGetter
public Map<String, Object> getUnknown() { // Keep this method for Jackson.
return this.unknown;
}
@SuppressWarnings("unused")
@JsonAnySetter
public void setUnknown(String name, Object value) { // Keep this method for Jackson.
this.unknown.put(name, value);
}
}
public class JSONExample {
public static void main(String[] args) throws JsonProcessingException {
final String jsonStr = """
{
"topKnownKey": 1,
"topUnknownKey": 2,
"nestedKnownArray": [
{
"nestedKnownKey": 3,
"nestedUnknownKey": 4
},
{
"nestedKnownKey": 5,
"nestedUnknownKey": 6
}
],
"nestedUnknownArray": [
{
"nestedKnownKey": 7,
"nestedUnknownKey": 8
}
]
}
""";
// Decode.
ObjectMapper om = new ObjectMapper();
Top top = om.readValue(jsonStr, Top.class);
// Modify.
top.topKnownKey = 100;
top.excludedKnownKey = 200;
top.nestedKnownArray.get(0).nestedKnownKey = 300;
top.nestedKnownArray.get(0).excludedKnownKey = 400;
// Encode.
DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
String jsonStrOut = om.writer(pp).writeValueAsString(top);
// Print output.
System.out.println(jsonStrOut);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment