Jackson-JsonLD嵌套对象

时间:2018-06-07 10:29:08

标签: java jackson schema.org json-ld

我想编写一个安静的API并使用schema.org注释我的数据。为此,我想使用Jackson-Jsonld。使用jackson-jsonld注释简单对象没有问题,但是嵌套对象的复杂对象让我陷入困境。在我的jsonld中,id,name这样的简单属性得到了反转,但嵌套的位置却没有。

我读到了序列化,它应该有助于获得第二个对象。但是,在实现我的序列化部分后,似乎序列化没有改变任何东西。 这是我的示例输出,location的类型应该是PostalAddress,但缺少类型:

{"@context":
    {"uri":"http://schema.org/url","name":"http://schema.org/name","location":"http://schema.org/location"},
    "@type":"http://schema.org/Organization",
    "uri":"http://localhost:8080/kangarooEvents/venue/12",
    "name":"Joondalup Library - Ground Floor Meeting Room",
    "location":{
         "address":"102 Boas Avenue",
         "city":"Joondalup",
         "zip":"6027",
         "country":"Australia",
         "state":"WA"},
    "@id":12}

我想要注释一个只​​有一个位置的组织:

@JsonldType("http://schema.org/Organization")
public class Venue {
    @JsonldId
    private Integer id;
    @JsonldProperty("http://schema.org/url")
    private String uri;
    @JsonldProperty("http://schema.org/name")
    private String name;
    @JsonSerialize(using = CostumLocationSerializer.class)
    @JsonldProperty("http://schema.org/location")
    private Location location;

地点:

@JsonldType("http://schema.org/PostalAddress")
public class Location {
    @JsonldProperty("http://schema.org/streetAddress")
    private String address;
    @JsonldProperty("http://schema.org/addressLocality")
    private String city;
    @JsonldProperty("http://schema.org/addressRegion")
    private String state;
    @JsonldProperty("http://schema.org/addressRegion")
    private String country;
    @JsonldProperty("http://schema.org/postalCode")
    private String zipcode;

序列化:

 public class CostumLocationSerializer extends StdSerializer<Location> {
      private ObjectMapper mapper = new ObjectMapper();

      public CostumLocationSerializer(){
         this( null);

      }
      protected CostumLocationSerializer(Class<Location> t) {
          super(t);
      }

      @Override
     public void serialize(Location location, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
         jsonGenerator.writeStartObject();
         jsonGenerator.writeStringField("address", location.getAddress());
         jsonGenerator.writeStringField("city", location.getCity());
         jsonGenerator.writeStringField("zip", location.getZipcode());
         jsonGenerator.writeStringField("country", location.getCountry());
         jsonGenerator.writeStringField("state", location.getState());
         jsonGenerator.writeEndObject();
         String serialized = mapper.writeValueAsString(location);
     } 
}

我认为我的问题可能在于序列化,但我无法弄清楚。也许有人注释了嵌套的obj。并告诉我我的问题是什么。

1 个答案:

答案 0 :(得分:0)

只需跳过jackson-jsonld部分并手动完成

  1. 创建JSON -只需将typeid的字段引入Java类即可。
  2. 创建JSON-LD上下文-将idtype字段映射到其他@context对象中
  3. 合并上下文和数据-例如只需使用标准的jackson API在“常规” json序列化之后添加@context对象即可。

示例

@Test
public void createJsonFromPojo() throws Exception {
    ObjectMapper mapper=new ObjectMapper();
    // Create object structure
    Venue venue = new Venue();
    venue.location = new Location();
    venue.id="12";
    venue.uri="http://localhost:8080/kangarooEvents/venue/12";
    venue.name="Joondalup Library - Ground Floor Meeting Room";
    venue.location.address="102 Boas Avenue";
    venue.location.city="Joondalup";
    venue.location.state="WA";
    venue.location.country="Australia";
    venue.location.zipcode="6027";

    //1. Create JSON 
    ObjectNode myData = mapper.valueToTree(venue);

    //2. Create a JSON-LD context
    ArrayNode context = mapper.createArrayNode();
    context.add("http://schema.org/");
    ObjectNode myContext=mapper.createObjectNode();
    myContext.put("id", "@id");
    myContext.put("type", "@type");
    context.add(myContext);

    //3. Combine context and data
    myData.set("@context",context);

    //4. Print
    StringWriter w = new StringWriter();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, myData);
    String result= w.toString();
    System.out.println(result);
}

public class Venue {
    public final String type = "http://schema.org/Organization";
    public String id;
    public String uri;
    public String name;
    public Location location;
}

public class Location {
    public final String type = "http://schema.org/PostalAddress";
    public String address;
    public String city;
    public String state;
    public String country;
    public String zipcode;
}

给你

{ 
    "@context": [
        "http://schema.org/",
        {
          "id": "@id",
          "type":"@type"
        }
     ],
     "uri":"http://localhost:8080/kangarooEvents/venue/12",
     "name":"Joondalup Library - Ground Floor Meeting Room",
     "location":{
         "address":"102 Boas Avenue",
         "city":"Joondalup",
         "zip":"6027",
         "country":"Australia",
         "state":"WA",
         "type":"http://schema.org/PostalAddress"
      },
      "id":"12",
      "type":"http://schema.org/Organization"
}

View Example in Playground