FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

Root name of JSON data format #36

Closed aufZone closed 10 years ago

aufZone commented 10 years ago

This is Person.java

@JsonRootName("Person")
public class Person {
    private String name;
    private double height;
    private int age;
    @JsonSerialize(using=CustomDateSerializer.class)
    private Date date;
    @JsonProperty("Address")
    private Address address;
        //normal constructor
        //getters/setters
}

This is my main class:

public class AppJson {
    public static void main(String[] arg) throws JsonGenerationException,
            JsonMappingException, IOException {
        Address a = new Address("Jln Koli", "90121", "Vila", "Belgium");
        Person p = new Person("Ali Bin Baba", new Date(), 90.0, 12, a);

        Json json = new Json(p);
        System.out.println(json.toString());
    }
}

Output:

{
  "name" : "Ali Bin Baba",
  "age" : 12,
  "date" : "06-12-2014",
  "Address" : {
    "postcode" : "90121",
    "city" : "Vila",
    "state" : "Belgium",
    "street" : "Jln Koli"
  }
}

But, the output I want is like this:

{
  "Person": {
    "name": "Ali Bin Baba",
    "age": "12",
    "date": "06-12-2014",
    "Address": {
      "postcode": "90121",
      "city": "Vila",
      "state": "Belgium",
      "street": "Jln Koli"
    }
  }
}

My dependencies:

<dependencies>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.3</version>
  </dependency>
</dependencies>

Did you know, which part is wrong??

cowtowncoder commented 10 years ago

When serialized as JSON, no additional wrapping is added, regardless of existence of @JsonRootName, unless explicitly defined to add wrapping. So you need to enable WRAP_ROOT_VALUE either globally:

mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

or for specific call using ObjectWriter

mapper.writer().with(SerializationFeature.WRAP_ROOT_VALUE).writeValue(...);
aufZone commented 10 years ago

It worked !!

Latest output:

{
  "Person" : {
    "name" : "Ali Bin Baba",
    "age" : 12,
    "date" : "06-13-2014",
    "Address" : {
      "postcode" : "90121",
      "city" : "Vila",
      "state" : "Belgium",
      "street" : "Jln Koli"
    }
  }
}

This my writer:

public class Json {
    private ByteArrayOutputStream baos = new ByteArrayOutputStream();

    private ObjectMapper mapper = new ObjectMapper();

    public Json(Object obj) throws JsonGenerationException,
            JsonMappingException, IOException {
        mapper.writerWithDefaultPrettyPrinter().with(SerializationFeature.WRAP_ROOT_VALUE).writeValue(baos, obj);
    }

    public int size() {
        return baos.size();
    }

    public String toString() {
        byte[] bytes = baos.toByteArray();
        return new String(bytes);
    }

}

Thank you very much!!

aufZone commented 10 years ago

Anyway, I have tried put that object into List like this:

Address a = new Address("Jln Koli", "90121", "Vila", "Belgium");
Person p = new Person("Ali Bin Baba", new Date(), 90.0, 12, a);
List<Person> persons = new LinkedList<>();
persons.add(p);
persons.add(p);
Json json = new Json(persons);
System.out.println(json.toString());

The output is like this:

{
  "LinkedList" : [ {
    "name" : "Ali Bin Baba",
    "age" : 12,
    "date" : "06-13-2014",
    "Address" : {
      "postcode" : "90121",
      "city" : "Vila",
      "state" : "Belgium",
      "street" : "Jln Koli"
    }
  }, {
    "name" : "Ali Bin Baba",
    "age" : 12,
    "date" : "06-13-2014",
    "Address" : {
      "postcode" : "90121",
      "city" : "Vila",
      "state" : "Belgium",
      "street" : "Jln Koli"
    }
  } ]
}

My question is, how to change root name "LinkedList" into "Person" ??

cowtowncoder commented 10 years ago

Two ways to go; either use something like:

@JsonRootName("Person")
public class PersonList extends ArrayList<Person> { }

to specify root name for your List subtype.

Or use method in ObjectWriter to use whatever name you want for object:

ObjectWriter w = ...;
w.withRootName("Person").writeValueAsString(list);
aufZone commented 10 years ago

Thank you. I'm use the first way, and it work!! Anyway, I'm also tried the second way, but it not work. This is my writer. What your suggestion?

public class Json {
    private ByteArrayOutputStream baos = new ByteArrayOutputStream();

    private ObjectMapper mapper = new ObjectMapper();

    public Json(Object obj) throws JsonGenerationException,
            JsonMappingException, IOException {
        mapper.writerWithDefaultPrettyPrinter()
                .with(SerializationFeature.WRAP_ROOT_VALUE)
                .writeValue(baos, obj);
    }

    public int size() {
        return baos.size();
    }

    public String toString() {
        byte[] bytes = baos.toByteArray();
        return new String(bytes);
    }

}
cowtowncoder commented 10 years ago

You are not using "withRootName("Person")" in there.

And from now on, let's use mailing lists for questions, discussions. Issue trackers are for bugs.