Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringified object representations don't preserve field order #88

Closed
longcao opened this issue Nov 4, 2015 · 2 comments
Closed

Stringified object representations don't preserve field order #88

longcao opened this issue Nov 4, 2015 · 2 comments

Comments

@longcao
Copy link
Contributor

longcao commented Nov 4, 2015

Currently, stringified io.circe.Json doesn't appear to preserve field order. This doesn't matter for machines but it would greatly help human readability.

scala> import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
import io.circe._
import io.circe.generic.auto._
import io.circe.jawn._
import io.circe.syntax._

scala> case class MyClass(a: Int, b: String, c: List[String], d: Boolean)
defined class MyClass

scala> val m = MyClass(5, "asdf", List("zxcv", "qwer", "hjkl"), false)
m: MyClass = MyClass(5,asdf,List(zxcv, qwer, hjkl),false)

scala> m.asJson
res0: io.circe.Json =
{
  "d" : false,
  "c" : [
    "zxcv",
    "qwer",
    "hjkl"
  ],
  "b" : "asdf",
  "a" : 5
}
@alexarchambault
Copy link
Contributor

@longcao Order is preserved internally, it's just that the default printers don't preserve it in their output, see https://github.com/travisbrown/circe/blob/master/core/shared/src/main/scala/io/circe/Printer.scala#L215 - it's the same as in argonaut.

The following gives back the right order for me:

Printer.spaces2.copy(preserveOrder = true).pretty(m.asJson)

giving

res1: String = """
{
  "a" : 5,
  "b" : "asdf",
  "c" : [
    "zxcv",
    "qwer",
    "hjkl"
  ],
  "d" : false
}
"""

@travisbrown
Copy link
Member

Fixed by #89—thanks @longcao, @alexarchambault!

julienrf pushed a commit to scalacenter/circe that referenced this issue May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants