JSON to pp::Var Converter
Convert JSON into pp::Var C++ notation for Pepper (NaCl/PPAPI) plugins. Paste JSON and get compilable pp::VarDictionary and pp::VarArray code instantly.
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.
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) →
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 [a−zA−Z0−9] with _ to produce valid C++ identifiers.
String escaping applies the C++ literal rules: \ → \\, " → \", newline → \n, tab → \t, carriage return → \r.
Reference Data
| JSON Type | C++ pp::Var Type | Constructor / Method | Example JSON | Example C++ Output |
|---|---|---|---|---|
| Object {} | pp::VarDictionary | pp::VarDictionary name; | {"a":1} | obj.Set("a",1); |
| Array [] | pp::VarArray | pp::VarArray name; | [1,2] | arr.Set(0,1); arr.Set(1,2); |
| String | pp::Var | Implicit from const char* | "hello" | "hello" |
| Number (int) | pp::Var | Implicit from int32_t | 42 | 42 |
| Number (float) | pp::Var | Implicit from double | 3.14 | 3.14 |
| Boolean TRUE | pp::Var | Implicit from bool | true | true |
| Boolean FALSE | pp::Var | Implicit from bool | false | false |
| NULL | pp::Var | pp::Var(pp::Var::Null()) | null | pp::Var(pp::Var::Null()) |
| Nested Object | pp::VarDictionary | Separate declaration, then .Set() | {"a":{"b":1}} | obj_a.Set("b",1); obj.Set("a",obj_a); |
| Nested Array | pp::VarArray | Separate declaration, then .Set() | {"a":[1]} | obj_a.Set(0,1); obj.Set("a",obj_a); |
| Array of Objects | pp::VarArray + pp::VarDictionary | Index-suffixed dictionary names | [{"x":1}] | arr_0.Set("x",1); arr.Set(0,arr_0); |
| Empty Object | pp::VarDictionary | Declaration only | {} | pp::VarDictionary obj; |
| Empty Array | pp::VarArray | Declaration only | [] | pp::VarArray arr; |
| Escaped String | pp::Var | Backslash-escaped const char* | "say \"hi\"" | "say \"hi\"" |
| Unicode String | pp::Var | UTF-8 passthrough | "café" | "café" |