简体   繁体   中英

Changing json libs to deserialize json array from com.fasterxml.jackson to org.codehaus.jackson?

I have to parse the following json response for "List Networks":

[
  {
    "Name": "bridge",
    "Id": "f2de39df4171b0dc801e8002d1d999b77256983dfc63041c0f34030aa3977566",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "Internal": false,
    "IPAM": {
      "Driver": "default",
      "Config": [
        {
          "Subnet": "172.17.0.0/16"
        }
      ]
    },
    "Containers": {
      "39b69226f9d79f5634485fb236a23b2fe4e96a0a94128390a7fbbcc167065867": {
        "EndpointID": "ed2419a97c1d9954d05b46e462e7002ea552f216e9b136b80a7db8d98b442eda",
        "MacAddress": "02:42:ac:11:00:02",
        "IPv4Address": "172.17.0.2/16",
        "IPv6Address": ""
      }
    },
    "Options": {
      "com.docker.network.bridge.default_bridge": "true",
      "com.docker.network.bridge.enable_icc": "true",
      "com.docker.network.bridge.enable_ip_masquerade": "true",
      "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
      "com.docker.network.bridge.name": "docker0",
      "com.docker.network.driver.mtu": "1500"
    }
  },
  {
    "Name": "none",
    "Id": "e086a3893b05ab69242d3c44e49483a3bbbd3a26b46baa8f61ab797c1088d794",
    "Scope": "local",
    "Driver": "null",
    "EnableIPv6": false,
    "Internal": false,
    "IPAM": {
      "Driver": "default",
      "Config": []
    },
    "Containers": {},
    "Options": {}
  },
  {
    "Name": "host",
    "Id": "13e871235c677f196c4e1ecebb9dc733b9b2d2ab589e30c539efeda84a24215e",
    "Scope": "local",
    "Driver": "host",
    "EnableIPv6": false,
    "Internal": false,
    "IPAM": {
      "Driver": "default",
      "Config": []
    },
    "Containers": {},
    "Options": {}
  }
]

Now, so far I have been using com.fasterxml.jackson to convert it to my desired Network.java pojo class and it was working as intended. Here is my old code:

public static List<Network> getNetworksFromResponse(String jsonInput, Network obj)
{
    ObjectMapper mapper = new ObjectMapper();
    try
    {
        return mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, Network.class));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return null;
}

where jsonInput is the above json response as a string. The problem here was that there is no root "Network" key to use a simple List networkList pojo. So I have to pass this entire json response into the method to return a list of networks.

Network.java:

public class Network
{
    private String EnableIPv6;
    private String Driver;
    private String Name;
    private String Labels;
    private String Internal;
    private String Id;
    private Options Options;
    private String Scope;
    private IPAM IPAM;

    public String getEnableIPv6()
    {
        return EnableIPv6;
    }

    @JsonProperty("EnableIPv6")
    public void setEnableIPv6(String EnableIPv6)
    {
        this.EnableIPv6 = EnableIPv6;
    }

    public String getDriver()
    {
        return Driver;
    }

    @JsonProperty("Driver")
    public void setDriver(String Driver)
    {
        this.Driver = Driver;
    }

    public String getName()
    {
        return Name;
    }

    @JsonProperty("Name")
    public void setName(String Name)
    {
        this.Name = Name;
    }

    public String getInternal()
    {
        return Internal;
    }

    @JsonProperty("Internal")
    public void setInternal(String Internal)
    {
        this.Internal = Internal;
    }

    public String getId()
    {
        return Id;
    }

    @JsonProperty("Id")
    public void setId(String Id)
    {
        this.Id = Id;
    }

    public Options getOptions()
    {
        return Options;
    }

    @JsonProperty("Options")
    public void setOptions(Options Options)
    {
        this.Options = Options;
    }

    public IPAM getIPAM()
    {
        return IPAM;
    }

    @JsonProperty("IPAM")
    public void setIPAM(IPAM IPAM)
    {
        this.IPAM = IPAM;
    }

    public String getScope()
    {
        return Scope;
    }

    @JsonProperty("Scope")
    public void setScope(String Scope)
    {
        this.Scope = Scope;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [EnableIPv6 = " + EnableIPv6 + ", Driver = " + Driver + ", Name = " + Name + ", Labels = " + Labels
                + ", Internal = " + Internal + ", Id = " + Id + ", Options = " + Options + ", IPAM = " + IPAM + ", Scope = "
                + Scope + "]";
    }
}

Now there are some other methods which are using org.codehaus library and there is a jar conflict coming. So I have to change my com.fasterxml.jackson to com.codehaus.jackson. I tried changing the import lines to codehaus libs, and it gives me this error:

The method getTypeFactory() is undefined for the type ObjectMapper

What is the equivalent method for this mapper.readValue(...) or getTypeFactory() method in codehaus? or what is the equivalent method for mapper.getTypeFactory().constructCollectionType(List.class, Network.class) in org.codehaus.jackson?

The answer for "going from com.fasterxml. Jackson to org.codehaus. Jackson" is usually "Don't". This because former means Jackson 2.x, maintained version with more functionality; and latter Jackson 1.x which hasn't been significantly update for past 4 years.

Having said that, Jackson 1.x ObjectMapper should have various readValue() methods, and can access TypeFactory . But it may be that you need to access it via config object; something like:

mapper.getSerializationConfig().getTypeFactory();

(check the spelling; names may vary slightly)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM