{ "summary": { "test1_id": "1444415", "test2_id": "4444935" }, "results": { "details": [ { "test1_id": "1444415", "test2_id": "4444935" }, { "test1_id": "1444415", "test2_id": "4444935" } ] } }
static class JsonContainer { DataContainer summary; ResultContainer results; } static class ResultContainer { List<DataContainer> details; } static class DataContainer { String test1_id; String test1_id; }
Gson gson = new GsonBuilder().create(); JsonContainer jsonContainer = gson.fromJson(json, JsonContainer.class);// Json Java ... // - String json = gson.toJson(jsonContainer);// Java json
static class JsonContainer { DataContainer summary; ResultContainer results; } static class ResultContainer { List<DataContainer> details; } static class DataContainer { @SerializedName("test1_id") String test1Id; @SerializedName("test2_id") String test2Id; }
Gson gson = new GsonBuilder().registerTypeAdapter(DataContainer.class, new DataContainerDeserializer<DataContainer>()).create(); class DataContainerDeserializer<T> implements JsonDeserializer<T> { @Override public T deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { ... // JsonElement return /* Java */ } } , JsonDeserializer . , GSON' .
package com.test; import com.google.common.collect.ObjectArrays; import com.google.gson.*; import com.google.gson.annotations.SerializedName; import gnu.trove.set.hash.THashSet; import javax.validation.constraints.NotNull; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.*; public class TestGson { private static String json = "{\n" + " \"summary\": {\n" + " \"test1_id\": \"1444415\",\n" + " \"test2_id\": \"4444935\"\n" + " },\n" + " \"results\": {\n" + " \"details\": [\n" + " {\n" + " \"test1_id\": \"1444415\",\n" + " \"test2_id\": \"4444935\"\n" + " },\n" + " {\n" + " \"test1_id\": \"1444415\",\n" + " \"test2_id\": \"4444935\"\n" + " }\n" + " ]\n" + " }\n" + "}"; public static void main(String [ ] args) { Gson gson = new GsonBuilder() .registerTypeAdapter(DataContainer.class, new VaidateDeserializer<DataContainer>()) // DataContainer .create(); JsonContainer jsonContainer = gson.fromJson(json, JsonContainer.class); } static class JsonContainer { DataContainer summary; ResultContainer results; } static class ResultContainer { List<DataContainer> details; } static class DataContainer { @NotNull @SerializedName("test1_id") String test1Id; @SerializedName("test2_id") String test2Id; } static class VaidateDeserializer<T> implements JsonDeserializer<T> { private Set<String> fields = null; // private Set<String> notNullFields = null; // NotNull private void init(Type type) { Class cls = (Class) type; Field[] fieldsArray = ObjectArrays.concat(cls.getDeclaredFields(), cls.getFields(), Field.class); // (, , fields = new THashSet<String>(fieldsArray.length); notNullFields = new THashSet<String>(fieldsArray.length); for(Field field: fieldsArray) { String name = field.getName().toLowerCase(); // Annotation[] annotations = field.getAnnotations(); // boolean isNotNull = false; for(Annotation annotation: annotations) { if(annotation instanceof NotNull) { // NotNull isNotNull = true; } else if(annotation instanceof SerializedName) { name = ((SerializedName) annotation).value().toLowerCase(); // SerializedName fields notNullFields } } fields.add(name); if(isNotNull) { notNullFields.add(name); } } } @Override public T deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { if(fields == null) { init(type); // } Set<Map.Entry<String, JsonElement>> entries = json.getAsJsonObject().entrySet(); Set<String> keys = new THashSet<String>(entries.size()); for (Map.Entry<String, JsonElement> entry : entries) { if(!entry.getValue().isJsonNull()) { // json, null keys.add(entry.getKey().toLowerCase()); // json } } if (!fields.containsAll(keys)) { // json, Java - throw new JsonParseException("Parse error! The json has keys that isn't found in Java object:" + type); } if (!keys.containsAll(notNullFields)) { // Java NotNull, json - throw new JsonParseException("Parse error! The NotNull fields is absent in json for object:" + type); } return new Gson().fromJson(json, type); // GSON } } }
JsonReader jsonReader = new JsonReader(reader); // reader, fileReader, json ,
- hasNext() - (, , ..) - peek() - (, , ..) - skipValue - - beginObject(), beginArray() - / - endObject(), endArray() - / - nextString() - - ..
Source: https://habr.com/ru/post/245263/
All Articles