You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@johnzon.apache.org by ravi sankar <sa...@yahoo.co.in.INVALID> on 2016/04/07 16:11:49 UTC

Re: Json Field Ordering

Hi ,
Continuing the discussion from here http://tomee-openejb.979440.n4.nabble.com/Json-Field-Ordering-td4678102.html.
I am attaching the sample code.
How to get the below output.
{    "c":"C",    "a":"A",    "e":"E"
}

Thanks,Ravi
 

    On Thursday, 7 April 2016 11:40 PM, Romain Manni-Bucau <rm...@gmail.com> wrote:
 

 Comparator was there for that exact purpose so not sure what you mean
byt it doesn't help. We regularly use that trick for tests to ensure
the ordering of fields in our asserts. Do you have some code which
doesn't work with that?

Side note: this belongs to johnzon list, not tomee.

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2016-04-07 14:56 GMT+02:00 ravi sankar <sa...@yahoo.co.in>:
> Hi Romain,
>
> Comparator doesn't help to achieve what I wanted do I have to register a
> custom adapter ? Could you please provide some example code to get what I
> wanted.
>
> Thanks,
> Ravi
>
>
> On Thursday, 7 April 2016 10:16 PM, Romain Manni-Bucau
> <rm...@gmail.com> wrote:
>
>
> Hi Ravi,
>
> First: there is no way to get the field ordering without parsing the
> java class since java 7 (reflection doesn't guarantee anything).
>
> Now about the feature why not just doing:
>
> final Mapper mapper = new MapperBuilder()
> .setAccessModeName("field")
> .addAdapter(new SimpleAdapter())
> .setAttributeOrder(new Comparator<String>() {
> @Override
> public int compare(final String o1, final String o2) {
> return o1.compareTo(o2); // whatever you need, here it sorts by alpha order
> }
> }).build();
>
> ?
>
>
>
>
> Romain Manni-Bucau
> @rmannibucau |  Blog | Github | LinkedIn | Tomitriber
>
>
> 2016-04-07 14:06 GMT+02:00 ravi sankar <sa...@yahoo.co.in.invalid>:
>> Hi ,
>>
>> Feature Request:
>>
>> class Test {
>>    private String c;
>>    private String a;
>>    private String e;
>> }
>>
>> I want json fields in the same order that I have defined in the class
>> file.
>> {
>>    "c":"1",
>>    "a":"2",
>>    "e":"3"
>> }
>>
>> Seems like doable using Johnzon.
>>
>> I tried something, I wouldn't say it is 100% correct but with minimal
>> changes it is possible.
>>
>> Mappings.java - Line number 347 HashMap to LinkedHashMap
>> FieldAccessMode.java - Line number 40,55 & 78 HashMap to LinkedHashMap
>> FieldAndMethodAccesMode.java - Line number 42 & 56 HashMap to
>> LinkedHashMap
>> MethodAccessMode.java- Line number 52 & 68 HashMap to LinkedHashMap
>> Also in MethodAccessMode.java getPropertyDescriptors function is changed a
>> little
>>
>> I am attaching the files
>>
>> Thanks,
>> Ravisankar Challa
>
>


  

Re: Json Field Ordering

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Ravi, you have to implement the comparator. We could surely support a
@JohnzonOrder(i) to make it easier by default but in your case you
create a list: list = Arrays.asList("c", "a", "e") and the comparator
does: list.indexOf(a) - list.indexOf(b)

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2016-04-07 16:11 GMT+02:00 ravi sankar <sa...@yahoo.co.in>:
> Hi ,
>
> Continuing the discussion from here
> http://tomee-openejb.979440.n4.nabble.com/Json-Field-Ordering-td4678102.html.
>
> I am attaching the sample code.
>
> How to get the below output.
> {
>     "c":"C",
>     "a":"A",
>     "e":"E"
> }
>
> Thanks,
> Ravi
>
>
> On Thursday, 7 April 2016 11:40 PM, Romain Manni-Bucau
> <rm...@gmail.com> wrote:
>
>
> Comparator was there for that exact purpose so not sure what you mean
> byt it doesn't help. We regularly use that trick for tests to ensure
> the ordering of fields in our asserts. Do you have some code which
> doesn't work with that?
>
> Side note: this belongs to johnzon list, not tomee.
>
> Romain Manni-Bucau
> @rmannibucau |  Blog | Github | LinkedIn | Tomitriber
>
>
> 2016-04-07 14:56 GMT+02:00 ravi sankar <sa...@yahoo.co.in>:
>> Hi Romain,
>>
>> Comparator doesn't help to achieve what I wanted do I have to register a
>> custom adapter ? Could you please provide some example code to get what I
>> wanted.
>>
>> Thanks,
>> Ravi
>>
>>
>> On Thursday, 7 April 2016 10:16 PM, Romain Manni-Bucau
>> <rm...@gmail.com> wrote:
>>
>>
>> Hi Ravi,
>>
>> First: there is no way to get the field ordering without parsing the
>> java class since java 7 (reflection doesn't guarantee anything).
>>
>> Now about the feature why not just doing:
>>
>> final Mapper mapper = new MapperBuilder()
>> .setAccessModeName("field")
>> .addAdapter(new SimpleAdapter())
>> .setAttributeOrder(new Comparator<String>() {
>> @Override
>> public int compare(final String o1, final String o2) {
>> return o1.compareTo(o2); // whatever you need, here it sorts by alpha
>> order
>> }
>> }).build();
>>
>> ?
>>
>>
>>
>>
>> Romain Manni-Bucau
>> @rmannibucau |  Blog | Github | LinkedIn | Tomitriber
>>
>>
>> 2016-04-07 14:06 GMT+02:00 ravi sankar
>> <sa...@yahoo.co.in.invalid>:
>>> Hi ,
>>>
>>> Feature Request:
>>>
>>> class Test {
>>>    private String c;
>>>    private String a;
>>>    private String e;
>>> }
>>>
>>> I want json fields in the same order that I have defined in the class
>>> file.
>>> {
>>>    "c":"1",
>>>    "a":"2",
>>>    "e":"3"
>>> }
>>>
>>> Seems like doable using Johnzon.
>>>
>>> I tried something, I wouldn't say it is 100% correct but with minimal
>>> changes it is possible.
>>>
>>> Mappings.java - Line number 347 HashMap to LinkedHashMap
>>> FieldAccessMode.java - Line number 40,55 & 78 HashMap to LinkedHashMap
>>> FieldAndMethodAccesMode.java - Line number 42 & 56 HashMap to
>>> LinkedHashMap
>>> MethodAccessMode.java- Line number 52 & 68 HashMap to LinkedHashMap
>>> Also in MethodAccessMode.java getPropertyDescriptors function is changed
>>> a
>>> little
>>>
>>> I am attaching the files
>>>
>>> Thanks,
>>> Ravisankar Challa
>>
>>
>
>