Add non-GC JSON stringify API ?
Categories
(Core :: JavaScript Engine, task, P3)
Tracking
()
People
(Reporter: arai, Unassigned)
References
Details
(derived from bug 1858692 comment #19 and some)
There are some consumers of JsonCpp in tree, which is for stringify-ing some data, such as principal.
one of the requirement there is "not using JSContext" (same as bug 1858803 for parsing API)
JsonCpp's stringify API requires creating a temporary object and call Json::writeString
with it,
and it's hitting perf issue (bug 1795312)
nsresult BasePrincipal::ToJSON(nsACString& aJSON) {
...
Json::Value root = Json::objectValue;
nsresult rv = ToJSON(root);
...
std::string result = Json::writeString(*sJSONBuilderForPrincipals, root);
For principal case, the input data is very simple, and it's not necessary to create temporary data if the JSON.stringify
operation can be performed with the following style:
JSONWriter writer(...);
writer.startObject();
writer.propertyName("foo");
writer.stringValue("bar");
...
writer.endObject();
auto resultJSONString = writer.finish();
This can be a set of reverse operations of the API added by bug 1858803 patch.
the new API just need to handle buffer allocation, concatenation, some state for object/array, and serialization of number and strings.
Reporter | ||
Comment 1•1 year ago
|
||
actually, we have https://searchfox.org/mozilla-central/source/mfbt/JSONWriter.h , which sounds like already implements the above.
maybe we could just utilize it, instead of implementing the same thing in JSAPI or wrapping it with JSAPI.
if the performance is more important, we could tweak it not to put indentation.
Reporter | ||
Comment 2•1 year ago
|
||
apparently mfbt/JSONWriter.h
is sufficient for the purpose, e.g. in bug 1861787
Description
•