如何在 App Engine 中将 json 字符串反序列化为 Java 对象

How do I deserialize a json String to Java Object in App Engine

我有一个 UI 发送这个对象 - com.google.api.services.compute.model.Network JSON 格式作为 HTTP 请求的一部分。我需要将此对象反序列化为 Java 对象。我怎么做?我写了一个简单的 POJO 来模仿下面的行为。

package com.techm.studio.client;

import java.io.IOException;

import com.google.api.services.compute.model.Network;
import com.google.api.services.compute.model.NetworkRoutingConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JSONTest {

    public static void main(String[] args) throws IOException {

        Network network = new Network();

        network.setName("k-network");
        network.setAutoCreateSubnetworks(false);
        network.setDescription("My test network on Google Cloud");
        network.setIPv4Range("10.0.0.0/16");
        network.setRoutingConfig(new NetworkRoutingConfig().setRoutingMode("REGIONAL"));

        String nwkJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(network);
        System.out.println("JSON String from POJO ==>\n" + nwkJsonString);

        Network netwrkOut = new Gson().fromJson(nwkJsonString, Network.class);
        System.out.println(netwrkOut.toPrettyString()); 
    }

}

这是我得到的输出。

JSON String from POJO ==>
{
  "IPv4Range": "10.0.0.0/16",
  "autoCreateSubnetworks": false,
  "description": "My test network on Google Cloud",
  "name": "k-network",
  "routingConfig": {
    "routingMode": "REGIONAL"
  }
}
Exception in thread "main" java.lang.IllegalArgumentException: Can not set com.google.api.services.compute.model.NetworkRoutingConfig field com.google.api.services.compute.model.Network.routingConfig to com.google.gson.internal.LinkedTreeMap
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:280)
    at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:241)
    at com.google.api.client.util.GenericData.put(GenericData.java:104)
    at com.google.api.client.util.GenericData.put(GenericData.java:48)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
    at com.google.gson.Gson.fromJson(Gson.java:887)
    at com.google.gson.Gson.fromJson(Gson.java:852)
    at com.google.gson.Gson.fromJson(Gson.java:801)
    at com.google.gson.Gson.fromJson(Gson.java:773)
    at com.techm.studio.client.JSONTest.main(JSONTest.java:25)

我不知道是否必须预先注册所有嵌套类型以及如何注册。

看起来 com.google.api.services.* 有它自己的 com.google.api.client.json.* 包,在这种情况下应该使用它来将模型序列化为 JSON 并将 JSON 反序列化为模型。请参阅以下示例:

import com.google.api.client.json.JsonGenerator;
import com.google.api.client.json.JsonParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.model.Network;
import com.google.api.services.compute.model.NetworkRoutingConfig;

import java.io.StringWriter;

public class GsonApp {

    public static void main(String[] args) throws Exception {
        Network network = new Network();

        network.setName("k-network");
        network.setAutoCreateSubnetworks(false);
        network.setDescription("My test network on Google Cloud");
        network.setIPv4Range("10.0.0.0/16");
        network.setRoutingConfig(new NetworkRoutingConfig().setRoutingMode("REGIONAL"));

        JacksonFactory jacksonFactory = new JacksonFactory();
        StringWriter writer = new StringWriter();
        try (JsonGenerator jsonGenerator = jacksonFactory.createJsonGenerator(writer)) {
            jsonGenerator.enablePrettyPrint();
            jsonGenerator.serialize(network);
        }

        String nwkJsonString = writer.toString();
        System.out.println("JSON String from POJO ==>\n" + nwkJsonString);

        JsonParser jsonParser = jacksonFactory.createJsonParser(nwkJsonString);
        Network netwrkOut = jsonParser.parse(Network.class);
        System.out.println(netwrkOut.toPrettyString());
    }
}

以上代码打印:

JSON String from POJO ==>
{
  "IPv4Range" : "10.0.0.0/16",
  "autoCreateSubnetworks" : false,
  "description" : "My test network on Google Cloud",
  "name" : "k-network",
  "routingConfig" : {
    "routingMode" : "REGIONAL"
  }
}
{
  "IPv4Range" : "10.0.0.0/16",
  "autoCreateSubnetworks" : false,
  "description" : "My test network on Google Cloud",
  "name" : "k-network",
  "routingConfig" : {
    "routingMode" : "REGIONAL"
  }
}

而且您不需要使用 Gson