User Rating 0.0
Total Usage 0 times
JSON Input
Ctrl + Enter
C++ pp::Var Output
Is this tool helpful?

Your feedback helps us improve.

About

Pepper Plugin API (PPAPI) requires structured data to be constructed manually using pp::VarDictionary and pp::VarArray objects. Hand-translating a JSON payload of 50+ keys into nested .Set() calls is tedious and error-prone. A misquoted string or a forgotten nested dictionary declaration produces compile errors that waste debugging cycles. This tool parses arbitrary JSON and emits correct, copy-pasteable C++ code that preserves the full tree structure, including deeply nested arrays and objects. It sanitizes key names into valid C++ identifiers and escapes string literals for the compiler.

Limitations: the converter assumes the target codebase includes the PPAPI headers (ppapi/cpp/var_dictionary.h, ppapi/cpp/var_array.h). JSON NULL values map to pp::Var(pp::Var::Null()). Numeric precision follows IEEE 754 double-precision rules inherent to both JSON and pp::Var(double). Integer values that exceed 231 1 are emitted as doubles.

json pp::Var C++ NaCl PPAPI Pepper converter code generator

Formulas

The converter performs a depth-first traversal of the parsed JSON tree. At each node, the algorithm determines the type and emits the corresponding C++ declaration or inline value.

convert(node, name)

{
emit pp::VarDictionary name; then for each (key, val) recurse & name.Set(key, ...) if node Objectemit pp::VarArray name; then for each index i recurse & name.Set(i, ...) if node Arrayinline value if node {String, Number, Boolean, NULL}

Variable naming follows the pattern: root = obj. For a nested key k inside parent p, the child variable becomes p_sanitize(k). For array index i, it becomes p_i. The sanitize function replaces any character not in [azAZ09] with _ to produce valid C++ identifiers.

String escaping applies the C++ literal rules: \ \\, " \", newline \n, tab \t, carriage return \r.

Reference Data

JSON TypeC++ pp::Var TypeConstructor / MethodExample JSONExample C++ Output
Object {}pp::VarDictionarypp::VarDictionary name;{"a":1}obj.Set("a",1);
Array []pp::VarArraypp::VarArray name;[1,2]arr.Set(0,1); arr.Set(1,2);
Stringpp::VarImplicit from const char*"hello""hello"
Number (int)pp::VarImplicit from int32_t4242
Number (float)pp::VarImplicit from double3.143.14
Boolean TRUEpp::VarImplicit from booltruetrue
Boolean FALSEpp::VarImplicit from boolfalsefalse
NULLpp::Varpp::Var(pp::Var::Null())nullpp::Var(pp::Var::Null())
Nested Objectpp::VarDictionarySeparate declaration, then .Set(){"a":{"b":1}}obj_a.Set("b",1); obj.Set("a",obj_a);
Nested Arraypp::VarArraySeparate declaration, then .Set(){"a":[1]}obj_a.Set(0,1); obj.Set("a",obj_a);
Array of Objectspp::VarArray + pp::VarDictionaryIndex-suffixed dictionary names[{"x":1}]arr_0.Set("x",1); arr.Set(0,arr_0);
Empty Objectpp::VarDictionaryDeclaration only{}pp::VarDictionary obj;
Empty Arraypp::VarArrayDeclaration only[]pp::VarArray arr;
Escaped Stringpp::VarBackslash-escaped const char*"say \"hi\"""say \"hi\""
Unicode Stringpp::VarUTF-8 passthrough"café""café"

Frequently Asked Questions

JSON null maps to pp::Var(pp::Var::Null()). The PPAPI specification defines Var::Null() as the canonical representation of an absent or undefined value. This differs from an uninitialized pp::Var, which defaults to Undefined type.
The converter handles arbitrary nesting depth. Variable names are constructed by concatenating parent names with underscore-separated child keys (e.g., obj_config_server_ssl_cert). Extremely deep nesting may produce long variable names, but these are valid C++ identifiers. If total name length exceeds practical readability, consider restructuring your JSON input.
Keys containing hyphens, spaces, dots, or other non-alphanumeric characters are sanitized for the C++ variable name by replacing each invalid character with an underscore. The original key string is preserved verbatim in the .Set() call's first argument, so the runtime key-value mapping remains correct.
Yes. If a JSON number has no fractional part and fits within the signed 32-bit integer range (−2,147,483,648 to 2,147,483,647), it is emitted as a plain integer literal. Numbers with decimal points or those exceeding int32 range are emitted as double literals. This matches the PPAPI overloaded constructors for pp::Var(int32_t) and pp::Var(double).
The generated code can be pasted directly into any function body that has access to the PPAPI C++ headers (ppapi/cpp/var.h, ppapi/cpp/var_dictionary.h, ppapi/cpp/var_array.h). You must ensure these headers are included at the top of your source file. The root variable is named "obj" for objects or "arr" for arrays - rename it as needed for your context.
Backslashes are doubled (\\), double quotes become \", newlines become \n, tabs become \t, and carriage returns become \r. This produces valid C++ string literals. Unicode characters (e.g., é, 日) are passed through as UTF-8, which is compatible with pp::Var's internal UTF-8 string storage.