Ejemplo n.º 1
0
void
orderly_free_json(orderly_alloc_funcs * alloc, orderly_json ** node)
{
    if (node && *node) {
        if ((*node)->k) OR_FREE(alloc, (void *) (*node)->k);

        if ((*node)->t == orderly_json_string) {
            OR_FREE(alloc, (void *) (*node)->v.s);
        } else if ((*node)->t == orderly_json_array ||
                   (*node)->t == orderly_json_object)
        {
            orderly_free_json(alloc, &((*node)->v.children.first));
        }
        if ((*node)->next) orderly_free_json(alloc, &((*node)->next));
        OR_FREE(alloc, (void *) (*node));        
    }
}
Ejemplo n.º 2
0
void ajv_free_node_state( const orderly_alloc_funcs * alloc, 
                          ajv_node_state *node)
{
  if (node && *node) {
    orderly_ps_free(alloc, (*node)->seen);
    orderly_ps_free(alloc, (*node)->required);
    OR_FREE(alloc,*node);
    *node = NULL;
  }
}
Ejemplo n.º 3
0
void ajv_clear_error (ajv_state s) {

  if (s->error.extra_info) OR_FREE(s->AF,s->error.extra_info);

  s->error.code       = ajv_e_no_error;
  s->error.extra_info = NULL;
  s->error.node       = NULL;


}
Ejemplo n.º 4
0
void
orderly_reader_free(orderly_reader *r)
{
    if (r && *r) {
        if ((*r)->node) {
            orderly_free_node(&((*r)->alloc), &((*r)->node));
        }
        OR_FREE(&((*r)->alloc), (*r));
        *r = NULL;
    }
}
Ejemplo n.º 5
0
void ajv_free(ajv_handle hand) {
  const orderly_alloc_funcs *AF = hand->AF;
  ajv_node_state        cur;
 
   ajv_clear_error(hand);
 
   orderly_free_node(hand->AF,(orderly_node **)&(hand->any.node));
  
  while (orderly_ps_length(hand->node_state)) {
    cur = orderly_ps_current(hand->node_state);
    ajv_free_node_state(hand->AF,&cur);
    orderly_ps_pop(hand->node_state);
  }
  orderly_ps_free(hand->AF,hand->node_state);

  orderly_free_node(hand->AF,(orderly_node **)&(hand->any.node));

  yajl_free(hand->yajl);
  OR_FREE(AF,hand);

}
Ejemplo n.º 6
0
void
orderly_lex_free(orderly_lexer lxr)
{
    OR_FREE(lxr->alloc, lxr);
    return;
}
Ejemplo n.º 7
0
void ajv_free_error(ajv_handle hand, unsigned char *str) {
  OR_FREE(hand->AF, str);
}
Ejemplo n.º 8
0
static orderly_json_parse_status
parse_json_schema(orderly_alloc_funcs * alloc,
                  orderly_json * j, orderly_node ** n)
{
    orderly_json_parse_status s = orderly_json_parse_s_ok;
    
    orderly_json * k;
    *n = NULL;
    if (j->t != orderly_json_object) {
        /* XXX: offset into the buffer!? */
        return orderly_json_parse_s_object_expected;
    }

    *n = orderly_alloc_node(alloc, orderly_node_empty);
    (*n)->additional_properties = orderly_node_any;

    for (k=j->v.children.first; k != NULL; k=k->next) {
        if (k->k != NULL) {
            if (!strcmp(k->k, "type")) {
                if (k->t == orderly_json_string) {
                    (*n)->t = orderly_string_to_node_type(k->v.s, strlen(k->v.s));
                    if ((*n)->t == orderly_node_empty) {
                        s = orderly_json_parse_s_invalid_type_value;
                        goto toErrIsHuman;
                    }
                } else if (k->t == orderly_json_array) {
                    /* support items containing an *array* of schema
                     * for tuple typing */
                    orderly_json * pj = NULL;
                    orderly_node ** last = &((*n)->child);
                    (*n)->t = orderly_node_union;

                    for (pj = k->v.children.first; pj; pj = pj->next)
                    {
                        s = parse_json_schema(alloc, pj, last);
                        if (s != orderly_json_parse_s_ok) {
                            goto toErrIsHuman;
                        }
                        last = &((*last)->sibling);
                    }
                } else {
                    s = orderly_json_parse_s_type_expects_string_or_array;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "properties")) {
                orderly_json * p = NULL;
                orderly_node ** last = &((*n)->child);

                if (k->t != orderly_json_object) {                
                    s = orderly_json_parse_s_invalid_properties_value;
                    goto toErrIsHuman;
                }

                for (p=k->v.children.first; p != NULL; p=p->next) {
                    orderly_node * pn = NULL;
                    s = parse_json_schema(alloc, p, &pn);
                    if (pn) {
                        *last = pn;
                        last = &(pn->sibling);
                        BUF_STRDUP(pn->name, alloc, p->k, strlen(p->k));
                    }
                    
                    if (s != orderly_json_parse_s_ok) {
                        goto toErrIsHuman;
                    }
                }
            }
            else if (!strcmp(k->k, "items")) {
                /* support items containing an *array* of schema
                *  for tuple typing */
                if (k->t == orderly_json_array) {
                    orderly_json * pj = NULL;
                    orderly_node ** last = &((*n)->child);
                    (*n)->tuple_typed = 1;
                    for (pj = k->v.children.first; pj; pj = pj->next)
                    {
                        s = parse_json_schema(alloc, pj, last);
                        if (s != orderly_json_parse_s_ok) {
                            goto toErrIsHuman;
                        }
                        last = &((*last)->sibling);
                    }
                } else if (k->t == orderly_json_object) {
                    orderly_node * pn = NULL;
                    s = parse_json_schema(alloc, k, &pn);
                    if (s != orderly_json_parse_s_ok) {
                        goto toErrIsHuman;
                    }
                    (*n)->child = pn;
                } else {
                    s = orderly_json_parse_s_items_gets_object_or_array;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "optional")) {
                if (k->t != orderly_json_boolean) {
                    s = orderly_json_parse_s_invalid_optional_value;
                    goto toErrIsHuman;
                }
                (*n)->optional = k->v.b;
            }
            else if (!strcmp(k->k, "minimum")) {
                if (k->t == orderly_json_integer) {
                    (*n)->range.info |= ORDERLY_RANGE_LHS_INT;
                    (*n)->range.lhs.i = k->v.i;
                } else if (k->t == orderly_json_number) {
                    (*n)->range.info |= ORDERLY_RANGE_LHS_DOUBLE;
                    (*n)->range.lhs.d = k->v.n;
                } else {
                    s = orderly_json_parse_s_minimum_requires_number;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "maximum")) {
                if (k->t == orderly_json_integer) {
                    (*n)->range.info |= ORDERLY_RANGE_RHS_INT;
                    (*n)->range.rhs.i = k->v.i;
                } else if (k->t == orderly_json_number) {
                    (*n)->range.info |= ORDERLY_RANGE_RHS_DOUBLE;
                    (*n)->range.rhs.d = k->v.n;
                } else {
                    s = orderly_json_parse_s_maximum_requires_number;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "minLength")) {
                if (k->t == orderly_json_integer) {
                    (*n)->range.info |= ORDERLY_RANGE_LHS_INT;
                    (*n)->range.lhs.i = k->v.i;
                } else {
                    s = orderly_json_parse_s_minlength_requires_integer;
                    goto toErrIsHuman;
                }

            }
            else if (!strcmp(k->k, "maxLength")) {
                if (k->t == orderly_json_integer) {
                    (*n)->range.info |= ORDERLY_RANGE_RHS_INT;
                    (*n)->range.rhs.i = k->v.i;
                } else {
                    s = orderly_json_parse_s_maxlength_requires_integer;
                    goto toErrIsHuman;
                }

            }
            else if (!strcmp(k->k, "minItems")) {
                if (k->t == orderly_json_integer) {
                    (*n)->range.info |= ORDERLY_RANGE_LHS_INT;
                    (*n)->range.lhs.i = k->v.i;
                } else {
                    s = orderly_json_parse_s_minitems_requires_integer;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "maxItems")) {
                if (k->t == orderly_json_integer) {
                    (*n)->range.info |= ORDERLY_RANGE_RHS_INT;
                    (*n)->range.rhs.i = k->v.i;
                } else {
                    s = orderly_json_parse_s_maxitems_requires_integer;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "additionalProperties")) {
                if (k->t == orderly_json_boolean) {
                    (*n)->additional_properties = 
                      k->v.b 
                      ? orderly_node_any
                      : orderly_node_empty;
                } else if (k->t == orderly_json_object
                           && !strcmp(k->v.children.first->k, "type")) {
                  if (k->v.children.first->t == orderly_json_string) {
                      (*n)->additional_properties = 
                        orderly_string_to_node_type(k->v.children.first->v.s, 
                                                    strlen(k->v.children.first->v.s));
                    } else {
                      s = orderly_json_parse_s_invalid_type_value;
                      goto toErrIsHuman;
                    }
                } else {
                  s = orderly_json_parse_s_addprop_requires_boolean;
                }
            }
            else if (!strcmp(k->k, "default")) {
                /* clone */
                (*n)->default_value = orderly_clone_json(alloc, k);
                /* remove the key */
                OR_FREE(alloc, (char *) (*n)->default_value->k);
                (*n)->default_value->k = NULL;

            }
            else if (!strcmp(k->k, "enum")) {
                /* clone */
                (*n)->values = orderly_clone_json(alloc, k);
                /* remove the key */
                OR_FREE(alloc, (char *) (*n)->values->k);
                (*n)->values->k = NULL;
            }
            else if (!strcmp(k->k, "pattern")) {
                if (k->t == orderly_json_string) {
                    pcre *regex;
                    const char *regerror;
                    int erroffset;
                    int error_code = 0;
                    BUF_STRDUP((*n)->regex, alloc, k->v.s, strlen(k->v.s));
                    regex = pcre_compile2((*n)->regex,
                                          0,
                                          &error_code,
                                          &regerror,
                                          &erroffset,
                                          NULL);
                    if (regex) {
                      pcre_free(regex);
                    }
                    if (error_code != 0) {
                      s = orderly_parse_s_regex_error + error_code;
                      goto toErrIsHuman;
                    }
                } else {
                    s = orderly_json_parse_s_pattern_requires_string;
                    goto toErrIsHuman;
                }
            }
            else if (!strcmp(k->k, "requires")) {
                if ((*n)->requires) {
                    s = orderly_json_parse_s_duplicate_requires;
                    goto toErrIsHuman;
                }
                
                if (k->t == orderly_json_string) {
                    (*n)->requires = OR_MALLOC(alloc, 2 * sizeof(char *));
                    BUF_STRDUP((*n)->requires[0], alloc, k->v.s, strlen(k->v.s));
                    (*n)->requires[1] = NULL;
                } else if (k->t == orderly_json_array) {
                    unsigned int num = 0;
                    orderly_json * ks;
                    
                    for (ks = k->v.children.first; ks; ks = ks->next)
                    {
                        unsigned int i;
                        char ** p;

                        if (ks->t != orderly_json_string) {
                            s = orderly_json_parse_s_requires_value_error;
                            goto toErrIsHuman;
                        }
                        num++;
                        p = OR_MALLOC(alloc, sizeof(char *) * (num + 1));
                        for (i = 0; i < num - 1; i++) p[i] = (char *) (*n)->requires[i];
                        BUF_STRDUP(p[i], alloc, ks->v.s, strlen(ks->v.s));
                        p[++i] = NULL;
                        if ((*n)->requires) OR_FREE(alloc, (*n)->requires);
                        (*n)->requires = (const char **) p;
                    }
                }
            }
            else if (!strcmp(k->k, "$schema")) {
              /* XXX: detect schema version, and store it somewhere!?,
                 then go back and adjust things */
              

            } else {
                orderly_json ** jPtr;
                    
                /* all unexpected properties are passed through using
                 * the passthrough_properties element of the node. */
                /* XXX: should this behavior be configurable? */
                if ((*n)->passthrough_properties == NULL) {
                    (*n)->passthrough_properties =
                        orderly_alloc_json(alloc, orderly_json_object);
                }
                jPtr = &((*n)->passthrough_properties->v.children.first);
                while (*jPtr) jPtr = &((*jPtr)->next);
                /* clone onto the passthrough object */
                (*jPtr) = orderly_clone_json(alloc, k);
            }
        }
    }
    
    /* json schema has some implied defaults, insert them */
    interject_defaults(alloc,*n);

    return s;

  toErrIsHuman:
    if (*n) orderly_free_node(alloc, n);
    return s;
}