user93796
user93796

Reputation: 18379

serializing array using jackson

I have a 2 classes as follows

class Rules{

 @JsonProperty("rules")
 Rule [] rules
}

class Rule{
 @JsonProperty("a1")
String attrubite1;
}

I have serialized this using json and it produces something like

{
    "rules": [{
       "a1": "somedataForRule1"
    }, {
        "a1": "somedataforTule1"
    }]
} 

I want the json to be like

{
    "rules": ["rule":{
       "a1": "somedataForRule1"
    }, 
    "rule":{
        "a1": "somedataforTule1"
    }]
} 

How do i do it?

MY code :

Rules rules = new Rules();
 rules.setRules(new Rule[]{r1,r2});
 Strings = objectMapper.writeValueAsString(rules);

Upvotes: 2

Views: 259

Answers (1)

afzalex
afzalex

Reputation: 8652

In arrays we cannot have keys. Arrays only have indexes as keys.
So the following is invalid json

[
    "a": {...}
]

To have keys we will need a map. So the following is valid.

{
    "a": {...}
}

So what you want is not possible, because you are asking jackson to create invalid json. Only way to hack this is to create your own utility.

Edit

Are u sure its invalid json?

www.json.org enter image description here

Upvotes: 1

Related Questions