JSON.stringify encodes JSON.Raw as string
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}}`
I ran into this as well. Map<string, JSON.Raw> doesn't work either (for stringify or parse).
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
Fixed. JSON.Raw is now it's own class, so it works anywhere.
Check the README for an example: https://github.com/JairusSW/json-as?tab=readme-ov-file#%EF%B8%8F-using-raw-json-strings