ankushg

    ankushg

    1 year ago
    Hey friends! Looking for some help on keeping a subset of JSON in string-form I have JSON like
    {
      "id": "file",
      "value": "File",
      "popup": {
        "menuitem": [
          {
            "value": "New",
            "onclick": "CreateDoc()"
          },
          {
            "value": "Open",
            "onclick": "OpenDoc()"
          },
          {
            "value": "Save",
            "onclick": "SaveDoc()"
          }
        ]
      }
    }
    I'd like to represent it with a
    data class Menu(val id: String, val value: String, val popup: String)
    where the
    popup
    subtree is stored as a String of valid JSON, instead of having to deserialize the whole nested
    popup
    structure. Happy to write a custom serializer for this because it doesn't happen too frequently in our codebase.
    ankushg

    ankushg

    1 year ago
    Ahh super unfortunate. Looks like a big issue for the original author of the issue was that they couldn't handle the memory pressure of
    JsonElement
    . I'm OK with using
    JsonElement
    behind the scenes, but still need the public API to be using
    String
    . Is this something a custom serializer could handle?
    e

    ephemient

    1 year ago
    if you're okay with that, serializing through a surrogate JsonElement should work
    quick hack
    ankushg

    ankushg

    1 year ago
    Nice I also threw something like that together myself! I'm assuming that typealiases are invisible to Serialization right? In other words, I can't just define a “typealias RawJson = String” and globally give it the surrogate serializer because it'll mess with regular Strings?
    e

    ephemient

    1 year ago
    correct, typealias is not a type you could define a value class though
    ankushg

    ankushg

    1 year ago
    Yeah that's the plan once I've migrated to JS IR 😄