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

JSON.stringify encodes JSON.Raw as string #98

Open
anisus opened this issue Nov 27, 2024 · 2 comments
Open

JSON.stringify encodes JSON.Raw as string #98

anisus opened this issue Nov 27, 2024 · 2 comments
Assignees
Labels
confirmed Seen by me, feature confirmed, or issue acknowledged enhancement New feature or request in-development Things I'm working on

Comments

@anisus
Copy link

anisus commented Nov 27, 2024

Issue

When encoding a value of type JSON.Raw using JSON.stringify, the value is encoded as if it was a string:

const raw: JSON.Raw = `{"foo":42}`;
const json = JSON.stringify(raw);
// Expected json to be: `{"foo":42}`
// but got: `"{\"foo\":42}"` 

Notes

I ran into the problem when making a generic function that took any type T that could be encoded to json:

function send<T>(value: T): void {
    const dta = JSON.stringify(value);
    /* ...  */
}

The issue does NOT occur if a class has a property of type JSON.Raw:

@json
class myClass {
    data: JSON.Raw = "null";
}
const cl = new myClass();
cl.data = `{"foo":42}`;
const json = JSON.stringify(cl);
// Results in expected json: `{"data":{"foo":42}}`
@mattjohnsonpint
Copy link
Contributor

I ran into this as well. Map<string, JSON.Raw> doesn't work either (for stringify or parse).

@JairusSW
Copy link
Owner

JairusSW commented Dec 6, 2024

I'll take a look into it--its likely going to be a pain to implement, but you should be able to expect it with the next release

@JairusSW JairusSW self-assigned this Jan 8, 2025
@JairusSW JairusSW added enhancement New feature or request in-development Things I'm working on confirmed Seen by me, feature confirmed, or issue acknowledged labels Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed Seen by me, feature confirmed, or issue acknowledged enhancement New feature or request in-development Things I'm working on
Projects
None yet
Development

No branches or pull requests

3 participants