Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Attributes | List of all members
skjson::Value Class Reference

#include <SkJSON.h>

Inheritance diagram for skjson::Value:
skjson::VectorValue< Value, Value::Type::kArray > skjson::VectorValue< Member, Value::Type::kObject > skjson::BoolValue skjson::NullValue skjson::NumberValue skjson::StringValue skjson::VectorValue< T, vtype > skjson::ArrayValue skjson::ObjectValue

Public Types

enum class  Type {
  kNull , kBool , kNumber , kString ,
  kArray , kObject
}
 

Public Member Functions

Type getType () const
 
template<typename T >
bool is () const
 
template<typename T >
const Tas () const
 
template<typename T >
 operator const T * () const
 
SkString toString () const
 
const Valueoperator[] (const char *key) const
 

Protected Types

enum class  Tag : uint8_t {
  kShortString = 0b00000000 , kNull = 0b00000001 , kBool = 0b00000010 , kInt = 0b00000011 ,
  kFloat = 0b00000100 , kString = 0b00000101 , kArray = 0b00000110 , kObject = 0b00000111
}
 

Protected Member Functions

void init_tagged (Tag)
 
void init_tagged_pointer (Tag, void *)
 
Tag getTag () const
 
template<typename T >
const Tcast () const
 
template<typename T >
Tcast ()
 
template<typename T >
const Tptr () const
 

Static Protected Attributes

static constexpr uint8_t kTagMask = 0b00000111
 

Detailed Description

A fast and likely non-conforming JSON parser.

Some known limitations/compromises:

– single-precision FP numbers

– missing string unescaping (no current users, could be easily added)

Values are opaque, fixed-size (64 bits), immutable records.

They can be converted to facade types for type-specific functionality.

E.g.:

if (v.is<ArrayValue>()) { for (const auto& item : v.as<ArrayValue>()) { if (const NumberValue* n = item) { printf("Found number: %f", **n); } } }

if (v.is<ObjectValue>()) { const StringValue* id = v.as<ObjectValue>()["id"]; if (id) { printf("Found object ID: %s", id->begin()); } else { printf("Missing object ID"); } }

Definition at line 57 of file SkJSON.h.

Member Enumeration Documentation

◆ Tag

enum class skjson::Value::Tag : uint8_t
strongprotected
Enumerator
kShortString 
kNull 
kBool 
kInt 
kFloat 
kString 
kArray 
kObject 

Definition at line 131 of file SkJSON.h.

131 : uint8_t {
132 // n.b.: we picked kShortString == 0 on purpose,
133 // to enable certain short-string optimizations.
134 kShortString = 0b00000000, // inline payload
135 kNull = 0b00000001, // no payload
136 kBool = 0b00000010, // inline payload
137 kInt = 0b00000011, // inline payload
138 kFloat = 0b00000100, // inline payload
139 kString = 0b00000101, // ptr to external storage
140 kArray = 0b00000110, // ptr to external storage
141 kObject = 0b00000111, // ptr to external storage
142 };

◆ Type

enum class skjson::Value::Type
strong
Enumerator
kNull 
kBool 
kNumber 
kString 
kArray 
kObject 

Definition at line 59 of file SkJSON.h.

59 {
60 kNull,
61 kBool,
62 kNumber,
63 kString,
64 kArray,
65 kObject,
66 };

Member Function Documentation

◆ as()

template<typename T >
const T & skjson::Value::as ( ) const
inline

Unguarded conversion to facade types.

Returns
The record cast as facade type T&.

Definition at line 85 of file SkJSON.h.

85 {
86 SkASSERT(this->is<T>());
87 return *reinterpret_cast<const T*>(this);
88 }
#define SkASSERT(cond)
Definition SkAssert.h:116
#define T

◆ cast() [1/2]

template<typename T >
T * skjson::Value::cast ( )
inlineprotected

Definition at line 194 of file SkJSON.h.

194{ return const_cast<T*>(const_cast<const Value*>(this)->cast<T>()); }

◆ cast() [2/2]

template<typename T >
const T * skjson::Value::cast ( ) const
inlineprotected

Definition at line 184 of file SkJSON.h.

184 {
185 static_assert(sizeof (T) <= sizeof(Value), "");
186 static_assert(alignof(T) <= alignof(Value), "");
187
188 return (sizeof(T) > sizeof(*this) / 2)
189 ? reinterpret_cast<const T*>(this) + 0 // need all the bits
190 : reinterpret_cast<const T*>(this) + 1; // skip the first word (where the tag lives)
191 }

◆ getTag()

Tag skjson::Value::getTag ( ) const
inlineprotected

Definition at line 148 of file SkJSON.h.

148 {
149 return static_cast<Tag>(fData8[0] & kTagMask);
150 }
static constexpr uint8_t kTagMask
Definition SkJSON.h:143

◆ getType()

Value::Type Value::getType ( ) const
inline
Returns
The type of this value.

Definition at line 371 of file SkJSON.h.

371 {
372 switch (this->getTag()) {
373 case Tag::kNull: return Type::kNull;
374 case Tag::kBool: return Type::kBool;
375 case Tag::kInt: return Type::kNumber;
376 case Tag::kFloat: return Type::kNumber;
377 case Tag::kShortString: return Type::kString;
378 case Tag::kString: return Type::kString;
379 case Tag::kArray: return Type::kArray;
380 case Tag::kObject: return Type::kObject;
381 }
382
383 SkASSERT(false); // unreachable
384 return Type::kNull;
385}
Tag getTag() const
Definition SkJSON.h:148

◆ init_tagged()

void Value::init_tagged ( Tag  t)
protected

Definition at line 38 of file SkJSON.cpp.

38 {
39 memset(fData8, 0, sizeof(fData8));
40 fData8[0] = SkTo<uint8_t>(t);
41 SkASSERT(this->getTag() == t);
42}

◆ init_tagged_pointer()

void Value::init_tagged_pointer ( Tag  t,
void *  p 
)
protected

Definition at line 45 of file SkJSON.cpp.

45 {
46 if (sizeof(Value) == sizeof(uintptr_t)) {
47 *this->cast<uintptr_t>() = reinterpret_cast<uintptr_t>(p);
48 // For 64-bit, we rely on the pointer lower bits being zero.
49 SkASSERT(!(fData8[0] & kTagMask));
50 fData8[0] |= SkTo<uint8_t>(t);
51 } else {
52 // For 32-bit, we store the pointer in the upper word
53 SkASSERT(sizeof(Value) == sizeof(uintptr_t) * 2);
54 this->init_tagged(t);
55 *this->cast<uintptr_t>() = reinterpret_cast<uintptr_t>(p);
56 }
57
58 SkASSERT(this->getTag() == t);
59 SkASSERT(this->ptr<void>() == p);
60}
void init_tagged(Tag)
Definition SkJSON.cpp:38

◆ is()

template<typename T >
bool skjson::Value::is ( ) const
inline
Returns
True if the record matches the facade type T.

Definition at line 77 of file SkJSON.h.

77{ return this->getType() == T::kType; }
Type getType() const
Definition SkJSON.h:371

◆ operator const T *()

template<typename T >
skjson::Value::operator const T * ( ) const
inline

Guarded conversion to facade types.

Returns
The record cast as facade type T*.

Definition at line 96 of file SkJSON.h.

96 {
97 return this->is<T>() ? &this->as<T>() : nullptr;
98 }

◆ operator[]()

const Value & Value::operator[] ( const char *  key) const
inline

Helper for fluent key lookup: v["foo"]["bar"]["baz"]

Returns
The lookup result value on success, otherwise NullValue.

Definition at line 387 of file SkJSON.h.

387 {
388 static const Value gNullValue = NullValue();
389
390 return this->is<ObjectValue>()
391 ? this->as<ObjectValue>()[key]
392 : gNullValue;
393}

◆ ptr()

template<typename T >
const T * skjson::Value::ptr ( ) const
inlineprotected

Definition at line 198 of file SkJSON.h.

198 {
199 static_assert(sizeof(uintptr_t) == sizeof(Value) ||
200 sizeof(uintptr_t) * 2 == sizeof(Value), "");
201
202 return (sizeof(uintptr_t) < sizeof(Value))
203 // For 32-bit, pointers are stored unmodified.
204 ? *this->cast<const T*>()
205 // For 64-bit, we use the lower bits of the pointer as tag storage.
206 : reinterpret_cast<T*>(*this->cast<uintptr_t>() & ~static_cast<uintptr_t>(kTagMask));
207 }
const T * cast() const
Definition SkJSON.h:184

◆ toString()

SkString Value::toString ( ) const
Returns
The string representation of this value.

Definition at line 938 of file SkJSON.cpp.

938 {
940 Write(*this, &wstream);
941 const auto data = wstream.detachAsData();
942 // TODO: is there a better way to pass data around without copying?
943 return SkString(static_cast<const char*>(data->data()), data->size());
944}
sk_sp< SkData > detachAsData()
Definition SkStream.cpp:707
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41

Member Data Documentation

◆ kTagMask

constexpr uint8_t skjson::Value::kTagMask = 0b00000111
inlinestaticconstexprprotected

Definition at line 143 of file SkJSON.h.


The documentation for this class was generated from the following files: