在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:EsotericSoftware/jsonbeans开源软件地址:https://github.com/EsotericSoftware/jsonbeans开源编程语言:Java 100.0%开源软件介绍:JsonBeansPlease use the JsonBeans discussion group for support. OverviewJsonBeans is a lightweight library that makes it easy to serialize and deserialize Java object graphs to and from JSON. The JAR is 45k and has no dependencies. Four small classes make up the important parts of the library:
Writing object graphsThe public class Person {
private String name;
private int age;
private ArrayList numbers;
}
public class PhoneNumber {
private String name;
private String number;
} Example object graph using these classes: Person person = new Person();
person.setName("Nate");
person.setAge(31);
ArrayList numbers = new ArrayList();
numbers.add(new PhoneNumber("Home", "206-555-1234"));
numbers.add(new PhoneNumber("Work", "425-555-4321"));
person.setNumbers(numbers); The JsonBeans code to serialize this object graph: Json json = new Json();
System.out.println(json.toJson(person)); {"numbers":[{"class":"com.example.PhoneNumber","name":"Home","number":"206-555-1234"},{"class":"com.example.PhoneNumber","name":"Work","number":"425-555-4321"}],"age":31,"name":"Nate"} That is compact, but hardly legible. The Json json = new Json();
System.out.println(json.prettyPrint(person)); {
"name": "Nate",
"age": 31,
"numbers": [
{
"name": "Home",
"class": "com.example.PhoneNumber",
"number": "206-555-1234"
},
{
"name": "Work",
"class": "com.example.PhoneNumber",
"number": "425-555-4321"
}
]
} Note that the class for the Json json = new Json();
json.setElementType(Person.class, "numbers", PhoneNumber.class);
System.out.println(json.prettyPrint(person)); {
"name": "Nate",
"age": 31,
"numbers": [
{
"name": "Home",
"number": "206-555-1234"
},
{
"name": "Work",
"number": "425-555-4321"
}
]
} When writing the class cannot be avoided, an alias can be given: Json json = new Json();
json.addClassTag("phoneNumber", PhoneNumber.class);
System.out.println(json.prettyPrint(person)); {
"name": "Nate",
"age": 31,
"numbers": [
{
"name": "Home",
"class": "phoneNumber",
"number": "206-555-1234"
},
{
"name": "Work",
"class": "phoneNumber",
"number": "425-555-4321"
}
]
} JsonBeans can write and read both JSON and a couple JSON-like formats. It supports "javascript", where the object property names are only quoted when needed. It also supports a "minimal" format, where both object property names and values are only quoted when needed. Json json = new Json();
json.setOutputType(OutputType.minimal);
json.setElementType(Person.class, "numbers", PhoneNumber.class);
System.out.println(json.prettyPrint(person)); {
name: Nate,
age: 31,
numbers: [
{
name: Home,
number: "206-555-1234"
},
{
name: Work,
number: "425-555-4321"
}
]
} Reading object graphsThe Json class uses reflection to automatically deserialize objects from JSON. Here is how to deserialize the JSON from the previous examples: Json json = new Json();
String text = json.toJson(person);
Person person2 = json.fromJson(Person.class, text); The type passed to Json json = new Json();
json.setOutputType(OutputType.minimal);
String text = json.toJson(person, Object.class);
System.out.println(json.prettyPrint(text));
Object person2 = json.fromJson(Object.class, text); {
class: com.example.Person,
name: Nate,
age: 31,
numbers: [
{
name: Home,
class: com.example.PhoneNumber,
number: "206-555-1234"
},
{
name: Work,
class: com.example.PhoneNumber,
number: "425-555-4321"
}
]
} To read the JSON as a DOM of maps, arrays, and values, the Json json = new Json();
String text = json.toJson(person, Object.class);
JsonValue root = new JsonReader().parse(text); The Customizing serializationSerialization can be customized by either having the class to be serialized implement the static public class PhoneNumber implements Json.Serializable {
private String name;
private String number;
public void write (Json json) {
json.writeValue(name, number);
}
public void read (Json json, JsonValue jsonMap) {
name = jsonMap.child().name();
number = jsonMap.child().asString();
}
}
Json json = new Json();
json.setElementType(Person.class, "numbers", PhoneNumber.class);
String text = json.prettyPrint(person);
System.out.println(text);
Person person2 = json.fromJson(Person.class, text); {
"name": "Nate",
"age": 31
"numbers": [
{
"Home": "206-555-1234"
},
{
"Work": "425-555-4321"
}
]
} In the
Json json = new Json();
json.setSerializer(PhoneNumber.class, new Json.Serializer<PhoneNumber>() {
public void write (Json json, PhoneNumber number, Class knownType) {
json.writeObjectStart();
json.writeValue(number.name, number.number);
json.writeObjectEnd();
}
public PhoneNumber read (Json json, JsonValue jsonData, Class type) {
PhoneNumber number = new PhoneNumber();
number.setName(jsonData.child().name());
number.setNumber(jsonData.child().asString());
return number;
}
});
json.setElementType(Person.class, "numbers", PhoneNumber.class);
String text = json.prettyPrint(person);
System.out.println(text);
Person person2 = json.fromJson(Person.class, text); Event based parsingThe |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论