IN2OSM  1.0.1
GenericSchemaDocument< ValueT, Allocator >

JSON schema document. More...

#include <fwd.h>

Collaboration diagram for GenericSchemaDocument< ValueT, Allocator >:
Collaboration graph

Classes

struct  SchemaEntry
 
struct  SchemaRefEntry
 

Public Types

typedef ValueT ValueType
 
typedef IGenericRemoteSchemaDocumentProvider< GenericSchemaDocumentIRemoteSchemaDocumentProviderType
 
typedef Allocator AllocatorType
 
typedef ValueType::EncodingType EncodingType
 
typedef EncodingType::Ch Ch
 
typedef internal::Schema< GenericSchemaDocumentSchemaType
 
typedef GenericPointer< ValueType, Allocator > PointerType
 
typedef GenericValue< EncodingType, Allocator > URIType
 

Public Member Functions

 GenericSchemaDocument (const ValueType &document, const Ch *uri=0, SizeType uriLength=0, IRemoteSchemaDocumentProviderType *remoteProvider=0, Allocator *allocator=0)
 Constructor. More...
 
 ~GenericSchemaDocument ()
 Destructor. More...
 
const URITypeGetURI () const
 
const SchemaTypeGetRoot () const
 Get the root schema. More...
 

Private Member Functions

 GenericSchemaDocument (const GenericSchemaDocument &)
 Prohibit copying. More...
 
GenericSchemaDocumentoperator= (const GenericSchemaDocument &)
 Prohibit assignment. More...
 
void CreateSchemaRecursive (const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
 
void CreateSchema (const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
 
bool HandleRefSchema (const PointerType &source, const SchemaType **schema, const ValueType &v, const ValueType &document)
 
const SchemaTypeGetSchema (const PointerType &pointer) const
 
PointerType GetPointer (const SchemaType *schema) const
 
const SchemaTypeGetTypeless () const
 

Private Attributes

IRemoteSchemaDocumentProviderTyperemoteProvider_
 
Allocator * allocator_
 
Allocator * ownAllocator_
 
const SchemaTyperoot_
 Root schema. More...
 
SchemaTypetypeless_
 
internal::Stack< Allocator > schemaMap_
 
internal::Stack< Allocator > schemaRef_
 
URIType uri_
 

Static Private Attributes

static const size_t kInitialSchemaMapSize = 64
 
static const size_t kInitialSchemaRefSize = 64
 

Friends

class internal::Schema< GenericSchemaDocument >
 
template<typename , typename , typename >
class GenericSchemaValidator
 

Detailed Description

template<typename ValueT, typename Allocator = CrtAllocator>
class GenericSchemaDocument< ValueT, Allocator >

JSON schema document.

A JSON schema document is a compiled version of a JSON schema. It is basically a tree of internal::Schema.

Note
This is an immutable class (i.e. its instance cannot be modified after construction).
Template Parameters
ValueTType of JSON value (e.g. Value ), which also determine the encoding.
AllocatorAllocator type for allocating memory of this document.

Definition at line 136 of file fwd.h.

Member Typedef Documentation

◆ AllocatorType

typedef Allocator AllocatorType

Definition at line 1505 of file schema.h.

◆ Ch

typedef EncodingType::Ch Ch

Definition at line 1507 of file schema.h.

◆ EncodingType

typedef ValueType::EncodingType EncodingType

Definition at line 1506 of file schema.h.

◆ IRemoteSchemaDocumentProviderType

◆ PointerType

typedef GenericPointer<ValueType, Allocator> PointerType

Definition at line 1509 of file schema.h.

◆ SchemaType

Definition at line 1508 of file schema.h.

◆ URIType

typedef GenericValue<EncodingType, Allocator> URIType

Definition at line 1510 of file schema.h.

◆ ValueType

typedef ValueT ValueType

Definition at line 1503 of file schema.h.

Constructor & Destructor Documentation

◆ GenericSchemaDocument() [1/2]

GenericSchemaDocument ( const ValueType document,
const Ch uri = 0,
SizeType  uriLength = 0,
IRemoteSchemaDocumentProviderType remoteProvider = 0,
Allocator *  allocator = 0 
)
inlineexplicit

Constructor.

Compile a JSON document into schema document.

Parameters
documentA JSON document as source.
uriThe base URI of this schema document for purposes of violation reporting.
uriLengthLength of name, in code points.
remoteProviderAn optional remote schema document provider for resolving remote reference. Can be null.
allocatorAn optional allocator instance for allocating memory. Can be null.

Definition at line 1525 of file schema.h.

1526  :
1527  remoteProvider_(remoteProvider),
1528  allocator_(allocator),
1529  ownAllocator_(),
1530  root_(),
1531  typeless_(),
1532  schemaMap_(allocator, kInitialSchemaMapSize),
1533  schemaRef_(allocator, kInitialSchemaRefSize)
1534  {
1535  if (!allocator_)
1536  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
1537 
1538  Ch noUri[1] = {0};
1539  uri_.SetString(uri ? uri : noUri, uriLength, *allocator_);
1540 
1541  typeless_ = static_cast<SchemaType*>(allocator_->Malloc(sizeof(SchemaType)));
1543 
1544  // Generate root schema, it will call CreateSchema() to create sub-schemas,
1545  // And call AddRefSchema() if there are $ref.
1546  CreateSchemaRecursive(&root_, PointerType(), document, document);
1547 
1548  // Resolve $ref
1549  while (!schemaRef_.Empty()) {
1550  SchemaRefEntry* refEntry = schemaRef_.template Pop<SchemaRefEntry>(1);
1551  if (const SchemaType* s = GetSchema(refEntry->target)) {
1552  if (refEntry->schema)
1553  *refEntry->schema = s;
1554 
1555  // Create entry in map if not exist
1556  if (!GetSchema(refEntry->source)) {
1557  new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(refEntry->source, const_cast<SchemaType*>(s), false, allocator_);
1558  }
1559  }
1560  else if (refEntry->schema)
1561  *refEntry->schema = typeless_;
1562 
1563  refEntry->~SchemaRefEntry();
1564  }
1565 
1566  RAPIDJSON_ASSERT(root_ != 0);
1567 
1568  schemaRef_.ShrinkToFit(); // Deallocate all memory for ref
1569  }
const SchemaType * root_
Root schema.
Definition: schema.h:1732
EncodingType::Ch Ch
Definition: schema.h:1507
object
Definition: rapidjson.h:646
internal::Stack< Allocator > schemaRef_
Definition: schema.h:1735
IRemoteSchemaDocumentProviderType * remoteProvider_
Definition: schema.h:1729
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1508
static const size_t kInitialSchemaMapSize
Definition: schema.h:1726
Allocator * ownAllocator_
Definition: schema.h:1731
SchemaType * typeless_
Definition: schema.h:1733
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:625
static const size_t kInitialSchemaRefSize
Definition: schema.h:1727
GenericPointer< ValueType, Allocator > PointerType
Definition: schema.h:1509
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1734
const SchemaType * GetSchema(const PointerType &pointer) const
Definition: schema.h:1710
void CreateSchemaRecursive(const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
Definition: schema.h:1634
Allocator * allocator_
Definition: schema.h:1730
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406

◆ ~GenericSchemaDocument()

~GenericSchemaDocument ( )
inline

Destructor.

Definition at line 1591 of file schema.h.

1591  {
1592  while (!schemaMap_.Empty())
1593  schemaMap_.template Pop<SchemaEntry>(1)->~SchemaEntry();
1594 
1595  if (typeless_) {
1596  typeless_->~SchemaType();
1597  Allocator::Free(typeless_);
1598  }
1599 
1601  }
Allocator * ownAllocator_
Definition: schema.h:1731
SchemaType * typeless_
Definition: schema.h:1733
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1734
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:629

◆ GenericSchemaDocument() [2/2]

GenericSchemaDocument ( const GenericSchemaDocument< ValueT, Allocator > &  )
private

Prohibit copying.

Member Function Documentation

◆ CreateSchema()

void CreateSchema ( const SchemaType **  schema,
const PointerType pointer,
const ValueType v,
const ValueType document 
)
inlineprivate

Definition at line 1651 of file schema.h.

1651  {
1652  RAPIDJSON_ASSERT(pointer.IsValid());
1653  if (v.IsObject()) {
1654  if (!HandleRefSchema(pointer, schema, v, document)) {
1655  SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_);
1656  new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true, allocator_);
1657  if (schema)
1658  *schema = s;
1659  }
1660  }
1661  }
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1508
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1734
bool HandleRefSchema(const PointerType &source, const SchemaType **schema, const ValueType &v, const ValueType &document)
Definition: schema.h:1663
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1181
Allocator * allocator_
Definition: schema.h:1730
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406

◆ CreateSchemaRecursive()

void CreateSchemaRecursive ( const SchemaType **  schema,
const PointerType pointer,
const ValueType v,
const ValueType document 
)
inlineprivate

Definition at line 1634 of file schema.h.

1634  {
1635  if (schema)
1636  *schema = typeless_;
1637 
1638  if (v.GetType() == kObjectType) {
1639  const SchemaType* s = GetSchema(pointer);
1640  if (!s)
1641  CreateSchema(schema, pointer, v, document);
1642 
1643  for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
1644  CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document);
1645  }
1646  else if (v.GetType() == kArrayType)
1647  for (SizeType i = 0; i < v.Size(); i++)
1648  CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document);
1649  }
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
object
Definition: rapidjson.h:646
array
Definition: rapidjson.h:647
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1508
void CreateSchema(const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
Definition: schema.h:1651
SchemaType * typeless_
Definition: schema.h:1733
const SchemaType * GetSchema(const PointerType &pointer) const
Definition: schema.h:1710
void CreateSchemaRecursive(const SchemaType **schema, const PointerType &pointer, const ValueType &v, const ValueType &document)
Definition: schema.h:1634
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1181
Allocator * allocator_
Definition: schema.h:1730
Here is the call graph for this function:

◆ GetPointer()

PointerType GetPointer ( const SchemaType schema) const
inlineprivate

Definition at line 1717 of file schema.h.

1717  {
1718  for (const SchemaEntry* target = schemaMap_.template Bottom<SchemaEntry>(); target != schemaMap_.template End<SchemaEntry>(); ++target)
1719  if (schema == target->schema)
1720  return target->pointer;
1721  return PointerType();
1722  }
GenericPointer< ValueType, Allocator > PointerType
Definition: schema.h:1509
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1734

◆ GetRoot()

const SchemaType& GetRoot ( ) const
inline

Get the root schema.

Definition at line 1606 of file schema.h.

1606 { return *root_; }
const SchemaType * root_
Root schema.
Definition: schema.h:1732

◆ GetSchema()

const SchemaType* GetSchema ( const PointerType pointer) const
inlineprivate

Definition at line 1710 of file schema.h.

1710  {
1711  for (const SchemaEntry* target = schemaMap_.template Bottom<SchemaEntry>(); target != schemaMap_.template End<SchemaEntry>(); ++target)
1712  if (pointer == target->pointer)
1713  return target->schema;
1714  return 0;
1715  }
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1734
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1181

◆ GetTypeless()

const SchemaType* GetTypeless ( ) const
inlineprivate

Definition at line 1724 of file schema.h.

1724 { return typeless_; }
SchemaType * typeless_
Definition: schema.h:1733

◆ GetURI()

const URIType& GetURI ( ) const
inline

Definition at line 1603 of file schema.h.

1603 { return uri_; }

◆ HandleRefSchema()

bool HandleRefSchema ( const PointerType source,
const SchemaType **  schema,
const ValueType v,
const ValueType document 
)
inlineprivate

Definition at line 1663 of file schema.h.

1663  {
1664  static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' };
1665  static const ValueType kRefValue(kRefString, 4);
1666 
1667  typename ValueType::ConstMemberIterator itr = v.FindMember(kRefValue);
1668  if (itr == v.MemberEnd())
1669  return false;
1670 
1671  if (itr->value.IsString()) {
1672  SizeType len = itr->value.GetStringLength();
1673  if (len > 0) {
1674  const Ch* s = itr->value.GetString();
1675  SizeType i = 0;
1676  while (i < len && s[i] != '#') // Find the first #
1677  i++;
1678 
1679  if (i > 0) { // Remote reference, resolve immediately
1680  if (remoteProvider_) {
1681  if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i)) {
1682  PointerType pointer(&s[i], len - i, allocator_);
1683  if (pointer.IsValid()) {
1684  if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) {
1685  if (schema)
1686  *schema = sc;
1687  new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(source, const_cast<SchemaType*>(sc), false, allocator_);
1688  return true;
1689  }
1690  }
1691  }
1692  }
1693  }
1694  else if (s[i] == '#') { // Local reference, defer resolution
1695  PointerType pointer(&s[i], len - i, allocator_);
1696  if (pointer.IsValid()) {
1697  if (const ValueType* nv = pointer.Get(document))
1698  if (HandleRefSchema(source, schema, *nv, document))
1699  return true;
1700 
1701  new (schemaRef_.template Push<SchemaRefEntry>()) SchemaRefEntry(source, pointer, schema, allocator_);
1702  return true;
1703  }
1704  }
1705  }
1706  }
1707  return false;
1708  }
const CharType(& source)[N]
Definition: pointer.h:1204
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
EncodingType::Ch Ch
Definition: schema.h:1507
internal::Stack< Allocator > schemaRef_
Definition: schema.h:1735
IRemoteSchemaDocumentProviderType * remoteProvider_
Definition: schema.h:1729
internal::Schema< GenericSchemaDocument > SchemaType
Definition: schema.h:1508
GenericPointer< ValueType, Allocator > PointerType
Definition: schema.h:1509
internal::Stack< Allocator > schemaMap_
Definition: schema.h:1734
bool HandleRefSchema(const PointerType &source, const SchemaType **schema, const ValueType &v, const ValueType &document)
Definition: schema.h:1663
JSON schema document.
Definition: fwd.h:136
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1181
Allocator * allocator_
Definition: schema.h:1730
virtual const SchemaDocumentType * GetRemoteDocument(const Ch *uri, SizeType length)=0

◆ operator=()

GenericSchemaDocument& operator= ( const GenericSchemaDocument< ValueT, Allocator > &  )
private

Prohibit assignment.

Friends And Related Function Documentation

◆ GenericSchemaValidator

friend class GenericSchemaValidator
friend

Definition at line 1513 of file schema.h.

◆ internal::Schema< GenericSchemaDocument >

friend class internal::Schema< GenericSchemaDocument >
friend

Definition at line 1511 of file schema.h.

Member Data Documentation

◆ allocator_

Allocator* allocator_
private

Definition at line 1730 of file schema.h.

◆ kInitialSchemaMapSize

const size_t kInitialSchemaMapSize = 64
staticprivate

Definition at line 1726 of file schema.h.

◆ kInitialSchemaRefSize

const size_t kInitialSchemaRefSize = 64
staticprivate

Definition at line 1727 of file schema.h.

◆ ownAllocator_

Allocator* ownAllocator_
private

Definition at line 1731 of file schema.h.

◆ remoteProvider_

IRemoteSchemaDocumentProviderType* remoteProvider_
private

Definition at line 1729 of file schema.h.

◆ root_

const SchemaType* root_
private

Root schema.

Definition at line 1732 of file schema.h.

◆ schemaMap_

internal::Stack<Allocator> schemaMap_
private

Definition at line 1734 of file schema.h.

◆ schemaRef_

internal::Stack<Allocator> schemaRef_
private

Definition at line 1735 of file schema.h.

◆ typeless_

SchemaType* typeless_
private

Definition at line 1733 of file schema.h.

◆ uri_

URIType uri_
private

Definition at line 1736 of file schema.h.


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