kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

JSON5 support

Open acanthite1855 opened this issue 5 years ago • 12 comments

What is your use-case and why do you need this feature? It would be nice to have JSON5 support to serialize/deserialize configs and other data files that may be written/read by humans. It has quite useful features:

  • No quotes for keys when they're not needed
  • Multi-line strings
  • Trailing comas
  • Single- and Multi-line comments.
  • and some more

Describe the solution you'd like Well, pretty much the same as for JSON but form JSON5. I don't know what else to say here. All details on the format are on json5.org

acanthite1855 avatar Apr 11 '20 14:04 acanthite1855

Side note: trailing commas and unquoted literals are now somewhat supported by isLenient = true setting

sandwwraith avatar Apr 13 '20 08:04 sandwwraith

Side note: IntelliJ has native support (e.g. syntax highlighting) for Json5 (link), which would make this a cool feature for Android developers especially

MarcelReiter avatar Sep 13 '21 12:09 MarcelReiter

now How is it going ?

lisonge avatar Dec 18 '21 17:12 lisonge

I'm also interested in this. For now, I've converted an existing JSON5 Java library to Kotlin and added some Kotlinx Serialization interop, so it can convert to/from JSON5/JSON. Please let me know if it's useful, and you're able to help make it more stable (I haven't tested it significantly)

https://github.com/adamko-dev/json5-kotlin

aSemy avatar Dec 22 '21 19:12 aSemy

Hi, any news about that ? Im trying to use json5, but it seems that kotlinx.serialization dont like comment

{
  /*
   * The day when players will be able to do damage to themselves. default -> 2
   */
  "dayOfAuthorizationOfThePvP": 2,

  /*
   * The day when players can get into the nether. default -> 4
   */
  "dayOfAuthorizationOfTheEntryInTheNether": 4,

  /*
   * If players can use enderpearl to assault bases or not
   */
  "allowEnderPearlAssault": false
}

error

Caused by: java.lang.RuntimeException: kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 11: Expected semicolon ':', but had ' ' instead at path: $
JSON input: {
  /*
   * The day when players will b.....
	at ch.skyfy.jsonconfig.example3.config.Configs.<clinit>(Configs.kt:25)
	... 90 more
Caused by: kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 11: Expected semicolon ':', but had ' ' instead at path: $

AmibeSkyfy16 avatar Jul 16 '22 07:07 AmibeSkyfy16

I don't think we'll embed JSON5 standard into our implementation. Current policy for format requests is 'write your own 3rd party lib with dependency on the core'

sandwwraith avatar Jul 19 '22 13:07 sandwwraith

I was faced with the need to deserialize JSON5 combined with a few additional requirements (such as full support for polymorphic types and error messages that can be exposed to the end user).

To meet these requirements, I created the json5k library. Feel free to take a look at it if you are in a similar situation.

Due to the fact that JSON5 is a superset of JSON, the library might also be suitable if you need to deserialize JSON files with trailing commas (#1812). In this case, however, you might be unable to reject inputs that violate other portions of the original JSON specification.

xn32 avatar Sep 20 '22 16:09 xn32

Thank you very much, I think that's what I need

AmibeSkyfy16 avatar Sep 21 '22 07:09 AmibeSkyfy16

Side note: trailing commas and unquoted literals are now somewhat supported by isLenient = true setting

For me trailing commas are throwing an error. What exactly does "somewhat" mean and is there a way to disable that check?

I tried out the json5k library and it seems to be great, but unfortunately it doesn't support iOS multiplatform targets.

ubuntudroid avatar Feb 01 '23 19:02 ubuntudroid

@ubuntudroid I've rechecked now — no, there's no way to disable trailing comma check even in lenient mode

sandwwraith avatar Feb 01 '23 20:02 sandwwraith

Alright, thanks for checking so quickly anyway! 🙏

ubuntudroid avatar Feb 01 '23 20:02 ubuntudroid

I can't parse API of this site cuz this library doesn't support trailing comma: https://developers.home-assistant.io/docs/intent_conversation_api/#response_types

GazimSoliev avatar Oct 01 '23 20:10 GazimSoliev