本文介绍了GSon属性允许原始或复合映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的模型中创建下面的json



要么我有

  {name:Arsénio,value:12} 

  {name:Arsénio,value:{min:12,max value:200}} 

我已经定义了以下POJO的

  class Data {
String name;
值值;
}

抽象类值{}
$ b $ class IntegerValue:Value {
int value;
}

class RangeValue:Value {
int max,min,value;
}

当使用IntegerValue时,显然这不会输出我所需的json因为它会输出

  Gson gson = new Gson(); 

Data data = new Data();
data.name =Arsénio;
data.value = new IntegerValue();
data.value.value = 12;

字符串结果= gson.toJson(data,Data.class);

System.out.println(result);

输出:

  {name:Arsénio,value:{value:12}} 

在这种情况下建模POJO的方法是什么?可以编写自定义 TypeAdapter a>:

  public class ValueTypeAdapter extends TypeAdapter< Value> {

@Override
public value read(JsonReader in)throws IOException {

Value value = null;

JsonParser jsonParser = new JsonParser();
JsonElement je = jsonParser.parse(in);

if(je instanceof JsonPrimitive){
value = new Value();
value.value =((JsonPrimitive)je).getAsInt();
} else if(je instanceof JsonObject){
JsonObject jsonObject =(JsonObject)je;
value = new Value();
value.max = jsonObject.get(max)。getAsInt();
value.min = jsonObject.get(min)。getAsInt();
value.value = jsonObject.get(value)。getAsInt();
}

返回值;
}

@Override
public void write(JsonWriter out,Value value)抛出IOException {
if(value!= null){
if( value.min == 0&& value.max == 0){
out.value(value.value);
} else {
out.beginObject();

out.name(min)。value(value.min);
out.name(max)。value(value.max);
out.name(value)。value(value.value);

out.endObject();
}
}
}
}

您的POJO类:

  public static class Data {
String name;
值值;
}

public static class Value {
int max,min,value;
}

测试: b

 字符串json0 ={\name \:\Arsénio\,\value \:12}; 
String json1 ={\name \:\Arsénio\,\value \:{\min \:12,\max \ :100,\value \:200}};

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Value.class,new ValueTypeAdapter());
Gson gson = gsonBuilder.create();

// 1)将json反序列化为java实例
Data data0 = gson.fromJson(json0,Data.class);
Data data1 = gson.fromJson(json1,Data.class);

// 2)将json序列化为java实例,以查看传入的json字符串等于序列化一个
String serialized0 = gson.toJson(data0);
String serialized1 = gson.toJson(data1);

System.out.println(serialized0:+ serialized0);
System.out.println(serialized1:+ serialized1);


I want to create the following json from my model

either i have

{"name" : "Arsénio", "value" : 12}

or

{"name" : "Arsénio", "value" : {"min" : 12, "max" : 100, "value" : 200}}

I've defined the following POJO's

class Data {
  String name;
  Value value;
}

abstract class Value {}

class IntegerValue : Value {
int value;
}

class RangeValue : Value {
    int max, min, value;
}

Obviously this won't output my required json for the first case when using IntegerValue since it will output

Gson gson = new Gson();

Data data = new Data();
data.name = "Arsénio";
data.value = new IntegerValue();
data.value.value = 12;

String result = gson.toJson(data, Data.class);

System.out.println(result);

Output:

{"name": "Arsénio", "value" : {"value" : 12}}

Whats the correct way to model my POJO's in this case ?

You can write a custom TypeAdapter:

public class ValueTypeAdapter extends TypeAdapter<Value> {

    @Override
    public Value read(JsonReader in) throws IOException {

        Value value = null;

        JsonParser jsonParser = new JsonParser();
        JsonElement je = jsonParser.parse(in);

        if (je instanceof JsonPrimitive) {
            value = new Value();
            value.value = ((JsonPrimitive) je).getAsInt();
        } else if (je instanceof JsonObject) {
            JsonObject jsonObject = (JsonObject) je;
            value = new Value();
            value.max = jsonObject.get("max").getAsInt();
            value.min = jsonObject.get("min").getAsInt();
            value.value = jsonObject.get("value").getAsInt();
        }

        return value;
    }

    @Override
    public void write(JsonWriter out, Value value) throws IOException {
        if (value != null) {
            if (value.min == 0 && value.max == 0) {
                out.value(value.value);
            } else {
                out.beginObject();

                out.name("min").value(value.min);
                out.name("max").value(value.max);
                out.name("value").value(value.value);

                out.endObject();
            }
        }
    }
}

Your POJO Classes:

public static class Data {
    String name;
    Value value;
}

public static class Value {
    int max, min, value;
}

Test:

    String json0 = "{\"name\" : \"Arsénio\", \"value\" : 12}";
    String json1 = "{\"name\" : \"Arsénio\", \"value\" : {\"min\" : 12, \"max\" : 100, \"value\" : 200}}";

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Value.class, new ValueTypeAdapter());
    Gson gson = gsonBuilder.create();

    // 1) Deserialize json to java instances
    Data data0 = gson.fromJson(json0, Data.class);
    Data data1 = gson.fromJson(json1, Data.class);

    // 2) Serialize json to java instances to see your incoming json string equals to seriallize one
    String serialized0 = gson.toJson(data0);
    String serialized1 = gson.toJson(data1);

    System.out.println("serialized0:" + serialized0);
    System.out.println("serialized1:" + serialized1);

这篇关于GSon属性允许原始或复合映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:02