Giter VIP home page Giter VIP logo

Comments (6)

cowtowncoder avatar cowtowncoder commented on August 29, 2024 1

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);

from jackson-annotations.

cowtowncoder avatar cowtowncoder commented on August 29, 2024

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(...);

from jackson-annotations.

aufZone avatar aufZone commented on August 29, 2024

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!!

from jackson-annotations.

aufZone avatar aufZone commented on August 29, 2024

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" ??

from jackson-annotations.

aufZone avatar aufZone commented on August 29, 2024

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);
    }

}

from jackson-annotations.

cowtowncoder avatar cowtowncoder commented on August 29, 2024

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.

from jackson-annotations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.