JohnWeisz/TypedJSON

Parsing array/set/map of primitive values

Closed this issue · 2 comments

Is it possible to deserialize collections of primitive values with typedjson without defining a schema? I mean something like this:

'[1, 2, 3, 4]' => number[]
'["a", "b", "c"]' => Set<string>
'{"a": 1, "b": 2, "c": 3}' => Map<string, number>

P.S. I know that it's possible to do something like this:

const arr: String[] = TypedJSON.parseAsArray('["a", "b", "c"]', String);

But under the hood it'll use primitive value constructors (Number/String, etc.) instead of primitive values themself.

Hi @rjkz808, why do you think it will use the constructors? By default, when TypedJSON sees a primitive it uses it directly (see deserializeDirectly function). The code below prints [ 'a', 'b', 'c' ], and true as expected.

        const result = TypedJSON.parseAsArray(
            '["a", "b", "c"]',
            String,
        );
        console.log(result);
        console.log(result[0] === 'a');