Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
More tricks!
final Map<String, Object> map = new HashMap<String, Object>();
JsonContext jsonContext = new JsonSerializer().createJsonContext(null);
BeanSerializer beanSerializer = new BeanSerializer(jsonContext, bean) {
@Override
protected void onSerializableProperty(
String propertyName, Class propertyType, Object value) {
map.put(propertyName, value);
}
};
beanSerializer.serialize();Generic JSON objects
JSONObject and JSONArray classes,StringBuilder sb = new StringBuilder();
JsonWriter jsonWriter = new JsonWriter(sb);
jsonWriter.writeOpenObject();
jsonWriter.writeName("one");
jsonWriter.writeNumber(Long.valueOf(123));
jsonWriter.writeComma();
jsonWriter.writeName("two");
jsonWriter.writeString("UberLight");
jsonWriter.writeCloseObject();JsonObject jo = JsonParser.create().parseAsJsonObject("{ ... }");
Integer i = jo.getInteger("key");
JsonObject child = jo.getJsonObject("child");
JsonArray ja = jo.getJsonArray("arr");
Double d = ja.getDouble(ndx);Book book = new Book();
book.setName("Jodd in Action);
book.setYear(2018);
book.setAuthors(List.of(new Author("Igor")));
String json = JsonSerializer.create()
.include("authors")
.serialize(book);{
"name" : "Jodd In Action",
"year" : 2018,
"authors" : [
{ "firstName" : "Igor" }
]
}Book book2 = new JsonParser()
.parse(json, Book.class);Tips on how to install Jodd JSON library in your app
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-json</artifactId>
<version>x.x.x</version>
</dependency>implementation 'org.jodd:jodd-json:x.x.x'implementation("org.jodd:jodd-json:x.x.x")libraryDependencies += "org.jodd" % "jodd-json" <dependency org="org.jodd" name="jodd-json" rev="x.x.x" />[org.jodd/jodd-json "x.x.x"]'org.jodd:jodd-json:jar:x.x.x'truepublic class Person {
private String name;
private Address home;
private Address work;
private List<Phone> phones = new ArrayList<Phone>();
// ... and getters and setters
}String json = new JsonSerializer
.include('phones')
.serialize(object);String json = new JsonSerializer
.exclude('work')
.include('phones')
.serialize(object);String json = JsonSerializer.create()
.exclude('work')
.include('phones')
.exclude('phones.areaCode')
.serialize(object);String json = new JsonSerializer
.exclude('*')
.include('name')
.serialize(object);public class Person {
private String name;
private Address home;
private Address work;
@JSON
private List<Phone> phones = new ArrayList<Phone>();
// ... getters and setters
}@JSON(strict = true)
public class Person {
@JSON
private String name;
@JSON
private Address home;
private Address work;
@JSON
private List<Phone> phones = new ArrayList<Phone>();
// ... and getters and setters
}public class Person {
@JSON
private String name;
@JSON(name = "home_address")
private Address home;
//...
}new JsonSerializer()
.excludeTypes(InputStream.class)
.serialize(object);new JsonSerializer()
.excludeTypes(InputStream.class)
.excludeTypes("javax.*")
.serialize(object);JsonSerializer jsonSerializer = new JsonSerializer();
String json = jsonSerializer.serialize(object);String json = JsonSerializer.create().serialize(object);String json = jsonSerializer.create().deep(true).serialize(object);JsonSerializer.create()
.setClassMetadataName("class")
.serialize(object);final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String json = new JsonSerializer()
.withSerializer("birthdate", new DateJsonSerializer() {
@Override
public void serialize(JsonContext jsonContext, Date date) {
jsonContext.writeString(dateFormat.format(date));
}
})
.serialize(foo);JsonSerializer serializer = new PrettyJsonSerializer();JsonSerializer.createPrettyOne()JsonParser jsonParser = new JsonParser();
Map map = jsonParser.parse(
"{ \"one\" : { \"two\" : 285 }, \"three\" : true}");Map map = JsonParser.create().parse("{ ... }");{
"name" : "Mak",
"bars" : {
"123": {"amount" : 12300},
"456": {"amount" : 45600}
},
"inters" : {
"letterJ" : {"sign" : "J"},
"letterO" : {"sign" : "O"},
"letterD" : {"sign" : "D"}
}
}public class User {
private String name;
private Map<String, Bar> bars;
private Map<String, Inter> inters;
// ...
}public class Bar {
private Integer amount;
// ...
}JsonParser jsonParser = new JsonParser();
User user = jsonParser.parse(json, User.class);public interface Inter {
public char getSign();
}public class InterImpl implements Inter {
protected char sign;
public char getSign() {
return sign;
}
public void setSign(char sign) {
this.sign = sign;
}
}User user = new JsonParser()
.map("inters.values", InterImpl.class)
.parse(json, User.class);String json = "{\"eee\" : {\"123\" : \"name\"}}";
Map<String, Map<Long, String>> map =
new JsonParser()
.map("values.keys", Long.class)
.parse(json); {
"1": {
"first": {
"areaCode": "404"
},
"second": {
"name": "Jodd"
}
}
}Map<String, Pair<Phone, Network>> complex = new JsonParser()
.map("values", Pair.class)
.map("values.first", Phone.class)
.map("values.second", Network.class)
.parse(json);Target target =
new JsonParser()
.setClassMetadataName("class")
.parse(json);final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Person person = new JsonParser()
.withValueConverter("birthdate", new ValueConverter<String, Date>() {
public Date convert(String data) {
try {
return dateFormat.parse(data);
} catch (ParseException pe) {
throw new JsonException(pe);
}
}
})
.parse(json, Person.class);JsonParser jsonParser = new JsonParser().looseMode(true);{
key : value,
'my' : 'hol\\x'
}JsonParser jsonParser = new JsonParser().lazy(true);JsonParser jsonParser = JsonParser.createLazyOne();