Fastest Way to check if v8::Local<v8::Value> is JSON serialisable or not

20 views
Skip to first unread message

Vinayaka Kamath

unread,
Jan 4, 2021, 1:06:18 AM1/4/21
to v8-users
Hello All,

I am looking for the fastest way to check if a v8::Local<v8::Value> is JSON serialisable or not.

Some negative cases are: undefined, {"foo": undefined}, Object({ })

Thanks!

Ben Noordhuis

unread,
Jan 4, 2021, 3:51:51 AM1/4/21
to v8-users
Simply trying to serialize it is really the only way from a
correctness perspective.

Objects can have .toJSON() methods (IOW, can execute arbitrary code
during serialization) so there is really no way to know what the
serialized object graph looks like until you try it.

By the way, all the negative cases you mention are in fact valid
inputs, they all produce an output when you pass them to
JSON.stringify().
Reply all
Reply to author
Forward
0 new messages