0

Can anyone tell why moshi toJSON giving alphabetical sorted JSON string

Model Class :

 Class { String firstName; String emailID;}

Resulting JSON:

{ "emailID" : someMail, "firstName" : name }
DbS
  • 1
  • 1
  • that's what the default reflection adapter does for Java classes. i'm not sure if the Java reflection APIs give any indication of field declaration order. Note that the Kotlin reflection adapter and the codegen adapters use property declaration order. – Eric Cochran Mar 13 '19 at 08:03
  • Is there any work around to have field declaration order? – DbS Mar 13 '19 at 18:34
  • If you are using Java, there is nothing built in to Moshi for this. You would need to write or generate your own JsonAdapters instead of using the default Java-reflection-based one. See auto-value-moshi for an example of such code generation in Java. – Eric Cochran Mar 13 '19 at 22:04
  • In my opinion you shouldn't write anything that would sort json objects - the spec says they have no particular sorting. In fact, you should not even care about the sitting. Any good json parser should know the sorting doesn't matter and therefore will work fine. Only json lists have a order. – Fred Mar 14 '19 at 11:47

0 Answers0