JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
79 stars 15 forks source link

Feature request: Support raw JSON string properties #88

Closed mattjohnsonpint closed 4 months ago

mattjohnsonpint commented 4 months ago

I need to be able to serialize/deserialize a class where some field is arbitrary JSON. For example, given this class:

@json
class Foo {
  value: string;
}

If I set bar to a string having the value {"a":1,"b":false,"c":"abc"} then normally when serializing Foo I would get:

{"value":"{\"a\":1,\"b\":false,\"c\":\"abc\"}"}

... which is correct, but in some cases I need it to just pass through the raw JSON so I can make the result be:

{"value":{"a":1,"b":false,"c":"abc"}}

I propose a @rawjson decorator:

@json
class Foo {
  @rawjson
  value: string;
}

Which would signal to the serialize and deserialize functions to just pass through anything in the string rather than attempt to encode or decode it.

Thanks.

mattjohnsonpint commented 4 months ago

It should also work with a string | null field, which would pass through null if set to null. And it should also work in conjunction with @alias, @omitnull, @omitif, etc. existing attributes.

JairusSW commented 4 months ago

Assign a property the type JSON.Raw to signify a block of Raw JSON. Example:

class Foo {
  bar: JSON.Raw;
}