CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/gason/gason.h
Views: 1401
#pragma once12#include <stdint.h>3#include <stddef.h>4#include <assert.h>56enum JsonTag {7JSON_NUMBER = 0,8JSON_STRING,9JSON_ARRAY,10JSON_OBJECT,11JSON_TRUE,12JSON_FALSE,13JSON_NULL = 0xF14};1516struct JsonNode;1718struct JsonValue {19union {20uint64_t ival_;21double fval_;22};23uint8_t tag_ = JSON_NULL;2425JsonValue(double x)26: fval_(x), tag_(JSON_NUMBER) {27}28JsonValue(JsonTag tag = JSON_NULL, void *payload = nullptr)29: ival_((uintptr_t)payload), tag_(tag) {30}31bool isDouble() const {32return tag_ == JSON_NUMBER;33}34JsonTag getTag() const {35return JsonTag(tag_);36}37uint64_t getPayload() const {38assert(!isDouble());39return ival_;40}41double toNumber() const {42assert(getTag() == JSON_NUMBER);43return fval_;44}45char *toString() const {46assert(getTag() == JSON_STRING);47return (char *)getPayload();48}49JsonNode *toNode() const {50assert(getTag() == JSON_ARRAY || getTag() == JSON_OBJECT);51return (JsonNode *)getPayload();52}53};5455struct JsonNode {56JsonValue value;57JsonNode *next;58char *key;59};6061struct JsonIterator {62JsonNode *p;6364void operator++() {65p = p->next;66}67bool operator!=(const JsonIterator &x) const {68return p != x.p;69}70JsonNode *operator*() const {71return p;72}73JsonNode *operator->() const {74return p;75}76};7778inline JsonIterator begin(JsonValue o) {79return JsonIterator{o.toNode()};80}81inline JsonIterator end(JsonValue) {82return JsonIterator{nullptr};83}8485#define JSON_ERRNO_MAP(XX) \86XX(OK, "ok") \87XX(BAD_NUMBER, "bad number") \88XX(BAD_STRING, "bad string") \89XX(BAD_IDENTIFIER, "bad identifier") \90XX(STACK_OVERFLOW, "stack overflow") \91XX(STACK_UNDERFLOW, "stack underflow") \92XX(MISMATCH_BRACKET, "mismatch bracket") \93XX(UNEXPECTED_CHARACTER, "unexpected character") \94XX(UNQUOTED_KEY, "unquoted key") \95XX(BREAKING_BAD, "breaking bad") \96XX(ALLOCATION_FAILURE, "allocation failure")9798enum JsonErrno {99#define XX(no, str) JSON_##no,100JSON_ERRNO_MAP(XX)101#undef XX102};103104const char *jsonStrError(int err);105106class JsonAllocator {107struct Zone {108Zone *next;109size_t used;110} *head;111112public:113JsonAllocator() : head(nullptr) {};114JsonAllocator(const JsonAllocator &) = delete;115JsonAllocator &operator=(const JsonAllocator &) = delete;116JsonAllocator(JsonAllocator &&x) noexcept : head(x.head) {117x.head = nullptr;118}119JsonAllocator &operator=(JsonAllocator &&x) noexcept {120head = x.head;121x.head = nullptr;122return *this;123}124~JsonAllocator() {125deallocate();126}127void *allocate(size_t size);128void deallocate();129};130131int jsonParse(char *str, char **endptr, JsonValue *value, JsonAllocator &allocator);132133134