This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
make— builds thejvaluedemo binary into the repo root viag++ -Wall -O2 -std=c++11 -Isrc. Object files go into.build/.make test— builds the test suite binary (.build/test, 59 test cases).make benchmark— builds the performance benchmark binary (.build/benchmark).make clean— removes.build/and thejvaluebinary.
Layout: the library is src/json.{h,cpp}; the demo main.cpp sits at the repo root; test and benchmark sources live in test/. All non-library sources compile with -Isrc.
This is a single-library, dependency-free C++11 project providing a dynamically-typed value container (jvalue) with serialization to/from JSON and Apple Property List formats (both XML plist and binary bplist). Despite the repo name, the library is not JSON-only — plist support is first-class and shares the same in-memory representation.
jvalue is a tagged union wrapping one of: null, int64, bool, double, string, array, object, date (time_t), or data (raw bytes). Key points future edits must preserve:
- Discriminant is
m_type(enumjtype); payload isunion _hold m_valueholding either a scalar or a heap pointer (p_string,p_array,p_object,p_data)._free()+_copy_value()are the only places that know the type-to-slot mapping; extending the type enum requires updating both, plus the move constructor/assignment. - Move semantics:
jvalue(jvalue&&)andoperator=(jvalue&&)transfer the union payload and reset the source toE_NULL. Zero-cost ownership transfer. E_DATEandE_DATAare plist-only concepts — JSON writers emit them as strings (ISO-ish date / base64).is_date_string()/is_data_string()heuristically detect these on read.- Implicit construction +
operator[]auto-vivifies objects/arrays (seemain.cppusage:jv["answer"]["everything"] = 66). Indexing a non-container mutates its type. Reads throughat()return the staticjvalue::nullsentinel on miss. json.hdeclaresusing namespace std;at file scope — propagated to every translation unit that includes it. Do not add symbols that would collide.
The object type uses a custom flat_map instead of std::map or std::unordered_map. It is defined as a nested class of jvalue but placed after jvalue's closing brace (to avoid incomplete-type issues).
- Dense
entry[]array — stores{string key, jvalue value}pairs in insertion order viamalloc+ placement new. Preserves key insertion order in JSON/plist output. - Adaptive hash index — when size reaches 32 keys, an
int32_t[]open-addressing hash table (FNV-1a, linear probing) is allocated automatically. Below 32 keys, lookups use linear scan (cache-friendly for small objects). - Manual memory management — uses
malloc/free+ placementnew/ explicit destructor calls. Copy/move constructors and assignment operators are fully implemented.
All four live in json.h and are implemented in the monolithic src/json.cpp. They are separate classes rather than methods on jvalue; jvalue::read / write / style_write / *_plist are thin facades over them.
jreader— recursive-descent JSON parser. Tracks position viam_pcursorand enforcesMAX_DEPTH = 256. Uses bulkappend()for string decoding and lookup-table_skip_spaces().jwriter— JSON serializer. Has a compactwritepath (withreserve(4096)) and a pretty-printingstyle_writepath that uses_is_multiline_arrayto decide inline vs. indented array layout.jpreader— Apple plist reader. Handles both XML plist and binary bplist (parseauto-detects,parse_binaryis the bplist-specific path). Thebplist_object_typeenum mirrors Apple's binary plist type markers (NS_NUMBER_INT = 0x10,NS_DATE = 0x30, etc.); keep those constants in sync with the bplist spec if touched. Uses_bp_in_bounds/_get_uint_val_safefor bounds-checked reads — all binary parsing must go through these helpers.jpwriter— plist writer, including binary bplist output._swapoverloads perform endian conversion (bplist is big-endian on disk);_write_value_to_binarybuilds a pool ofbplist_objects first, then emits the offset table and trailer.
jbase64 provides encode/decode used by jvalue::assign_data / as_data and the plist data-tag path. It is declared at the end of json.h and implemented at the end of json.cpp (the library is fully contained in those two files). It caches allocated buffers in m_array_encodes / m_array_decodes and frees them in the destructor — callers hold pointers only for the lifetime of the jbase64 instance.
test/test.cpp— 59 test cases covering construction, copy/move, JSON parse/write, plist roundtrip, object/array operations, type conversions, edge cases. Run viamake test && ./.build/test.test/benchmark.cpp— large-scale performance benchmark (200-user nested JSON, 2000-element arrays, 100/500-device plists with binary data and dates). Run viamake benchmark && ./.build/benchmark.