3

I know how to add a property using jackson with ObjectNode.put() but is there a way to insert the property at a specific position? For example if I have this JsonNode:

{
    "property1":"val1",
    "property2":"val2",
    "property3":"val3"
}

What I want is:

{
    "property1":"val1",
    "property4":"val4",
    "property2":"val2",
    "property3":"val3"
}

How can I achieve that with Jackson?

SHIYU ZHANG
  • 41
  • 1
  • 5

2 Answers2

2

The JSON spec does not require the objects to be ordered (they may or may not be depending on the implementation), so you should not rely on it. Arrays, however, are explicitly ordered:

6 Objects

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs. A name is a string . A single colon token follows each name, separating the name from the value . A single comma token separates a value from a following name.

7 Arrays

An array structure is a pair of square bracket tokens surrounding zero or more values . The values are separated by commas. The order of the values is significant.

(from http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf)

Dici
  • 25,226
  • 7
  • 41
  • 82
  • Oh okay, now I see that the order of properties doesn't really matter. What I'm trying to do is to read a json formatted file, add some properties at a specific position, write back and save the file. The file is well formatted and used for configuration, I don't want to make it messy, so that's why I want to put properties in order. – SHIYU ZHANG May 07 '16 at 22:20
  • The file is about 37 MB, should be ok to copy it. – SHIYU ZHANG May 07 '16 at 22:22
  • I was just thinking that since Jackson internally uses `LinkedHasMap` you could re-populate the whole object every time you want to insert into it somewhere else than at the end. However, 37 MB is not so small and it would be inefficient. By the way, it might stop working at the next version of Jackson or using another JSON library. All that to say I don't recommend it. Maybe JSON is not the format you want, or maybe you need arrays instead of objects – Dici May 07 '16 at 22:31
2

Use an ObjectMapper and either use @JsonPropertyOrder(alphabetic=true) or an explicitly-specified ordering.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Oh good, I guess `ObjectMapper` is what I want, I'll give a try, thanks! – SHIYU ZHANG May 07 '16 at 22:25
  • That's nice, but it's assuming that his config is held in a single class, which might not be the case, it might just be data for example. In this case, he would have to write a 100% boilerplate class, which might be acceptable but is arguably not great – Dici May 07 '16 at 22:35
  • Hi Matt, is it possible to handle it without serialization? To be clear, what I want to do is to read a json file, insert a property then write back to the file. – SHIYU ZHANG May 09 '16 at 19:58
  • What do you mean by "without serialization?" Writing a file as JSON _is_ serialization. – Matt Ball May 09 '16 at 20:00
  • Hmm... okay, from my understanding, I need to create classes to handle the data if I want to write back. Is that true? For example if I have a car object, I need to have a car class to handle the data, then modify data and write back to the file. Can I just modify the json file without those classes? – SHIYU ZHANG May 10 '16 at 17:43