Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 327 lines (294 sloc) 10.048 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
// Copyright (c) 2012, Yahoo! Inc. All rights reserved.
// Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.

#include <gearbox/core/Json.h>
#include <gearbox/core/JsonParser.h>
#include <gearbox/core/JsonSchema.h>
#include <gearbox/core/Errors.h>

#include "JsonPrivate.h"

#include <boost/lexical_cast.hpp>

#include <string>
using std::string;

namespace {
    struct YajlHandleAutoPtr {
        YajlHandleAutoPtr(yajl_handle handle) : handle_(handle) {}
        ~YajlHandleAutoPtr() { if(handle_) yajl_free(handle_); }
        yajl_handle get() { return handle_; }
        yajl_handle handle_;
    };
}

namespace Gearbox {

void Json::Parser::parse(
    const string & content,
    Json & json,
    JsonSchema * schema
) {
    json.freeData();
    Json::Parser::Context ctx;
    ctx.cursor = &json;
    ctx.schema = schema;
    
    YajlHandleAutoPtr yhap(yajl_alloc(&Json::Parser::callbacks, NULL, &ctx));
    if( !yhap.get() ) {
        gbTHROW( JsonError(json.context(), "failed to create parser") );
    }
    yajl_config(yhap.get(), yajl_allow_comments, 1);
    yajl_status stat = yajl_parse(yhap.get(), reinterpret_cast<const unsigned char *>(content.c_str()), content.size());
    if( stat == yajl_status_ok ) {
        stat = yajl_complete_parse(yhap.get());
    }
    
    if( !ctx.exception.empty() ) {
        gbTHROW( JsonError(ctx.exception) );
    }

    if (stat != yajl_status_ok) {
        unsigned char * str = yajl_get_error(yhap.get(), 1, reinterpret_cast<const unsigned char *>(content.c_str()), content.size());
        string err(reinterpret_cast<const char *>(str));
        yajl_free_error(yhap.get(), str);
        _WARN("failed to parse content <<EOM\n" << content << "\nEOM");
        gbTHROW( JsonError(json.context(), "parse error: " + err) );
    }
}

Json * Json::Parser::getJson(void * ctx) {
    return static_cast< Json::Parser::Context *>(ctx)->cursor;
}

void Json::Parser::setJson(void * ctx, Json * j) {
    static_cast< Json::Parser::Context *>(ctx)->cursor = j;
}

void Json::Parser::setJsonUp(void * ctx) {
    static_cast< Json::Parser::Context *>(ctx)->cursor
        = static_cast< Json::Parser::Context *>(ctx)->cursor->impl_->parent;
    setSchemaUp(ctx);
}

JsonSchema * Json::Parser::getSchema(void * ctx) {
    return static_cast< Json::Parser::Context *>(ctx)->schema;
}

void Json::Parser::setSchema(void * ctx, JsonSchema * s) {
    assert(s);
    static_cast< Json::Parser::Context *>(ctx)->schema = s;
}

void Json::Parser::setSchemaUp(void * ctx) {
    if( static_cast< Json::Parser::Context *>(ctx)->schema ) {
        static_cast< Json::Parser::Context *>(ctx)->schema =
            static_cast<JsonSchema*>(static_cast< Json::Parser::Context *>(ctx)->schema->impl_->parent);
    }
}

Json * Json::Parser::getObj(void * ctx) {
    Json * cur = getJson(ctx);
    if( cur->impl_->type == Json::ARRAY ) {
        boost::shared_ptr<Json> j(new Json);
        j->impl_->parent = cur;
        cur->as<Json::Array>().push_back( j );
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            int newIdx = cur->as<Json::Array>().size();
            if( ! s->validMaxSize(*cur) ) return NULL;
            JsonSchema * child = s->getChildSchema(newIdx - 1, *cur);
            if( !child ) {
                return NULL;
            }
            setSchema(ctx, child);
        }
        setJson(ctx, j.get());
    }
    return getJson(ctx);
}

int Json::Parser::parse_null(void * ctx) {
    try {
        Json * cur = getObj(ctx);
        if( !cur ) return 0;
        assert(cur->impl_->type == Json::UNDEF);
        if( getSchema(ctx) ) {
            if( ! getSchema(ctx)->validType(*cur) ) return 0;
        }
        setJsonUp(ctx);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception= err.what();
        return 0;
    }
}

int Json::Parser::parse_boolean(void * ctx, int boolVal) {
    try {
        Json * cur = getObj(ctx);
        if( !cur ) return 0;
        assert(cur->impl_->type == Json::UNDEF);
        cur->setData(new bool(boolVal), Json::BOOL);
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            bool cont = s->validType(*cur)
                && s->validValue(static_cast<bool>(boolVal));
            if(!cont) return 0;
        }
        setJsonUp(ctx);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception= err.what();
        return 0;
    }
}

int Json::Parser::parse_number(void *ctx, const char *numberVal, size_t numberLen) {
    try {
        Json * cur = getObj(ctx);
        if(!cur) return 0;
        string strnum(numberVal, numberLen);
        if( strnum.find('.') == string::npos ) {
            // no dot, so assume it is integer with optional exponent
            cur->setData( new int64_t( boost::lexical_cast<int64_t>(strnum) ), Json::INT );
        }
        else {
            // has dot so assume double
            cur->setData( new double( boost::lexical_cast<double>(strnum) ), Json::DOUBLE );
        }
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            bool cont = s->validType(*cur)
                && s->validValue(*cur);
            if( !cont ) {
                return 0;
            } else {
                s->typeFixup( *cur );
            }
        }
        setJsonUp(ctx);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception= err.what();
        return 0;
    }
}

int Json::Parser::parse_string(void * ctx, const unsigned char * stringVal,
                        size_t stringLen) {
    try {
        Json * cur = getObj(ctx);
        if( !cur ) return 0;
        assert(cur->impl_->type == Json::UNDEF);
        std::auto_ptr<std::string> value(new string(reinterpret_cast<const char*>(stringVal), stringLen));
        cur->setData(value.release(), Json::STRING);
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            bool cont = s->validType(*cur)
                && s->validValue(*cur);
            if(!cont) return 0;
        }
        setJsonUp(ctx);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception= err.what();
        return 0;
    }
}

int Json::Parser::parse_start_map(void * ctx) {
    try {
        Json * cur = getObj(ctx);
        if( !cur ) return 0;
        assert(cur->impl_->type == Json::UNDEF);
        cur->setData(new Json::Object(), Json::OBJECT);
        if( getSchema(ctx) ) {
            if( !getSchema(ctx)->validType(*cur) ) return 0;
        }
        setJson(ctx, cur);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception = err.what();
        return 0;
    }
}

int Json::Parser::parse_map_key(void * ctx, const unsigned char * key,
                         size_t stringLen) {
    try {
        assert(getJson(ctx)->impl_->type == Json::OBJECT);
        string name(reinterpret_cast<const char*>(key), stringLen);
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            JsonSchema * child = s->getChildSchema(name, *(getJson(ctx)));
            if( !child ) {
                return 0;
            }
            setSchema(ctx,child);
        }
        boost::shared_ptr<Json> j(new Json);
        Json::Object &map = getJson(ctx)->as<Json::Object>();
        if ( map.find( name ) != map.end() ) {
            gbTHROW( std::runtime_error("duplicate key name found: " + name) );
        }
        map.insert ( Json::Object::value_type(name, j) );
        j->impl_->parent = getJson(ctx);
        setJson(ctx, j.get());
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception = err.what();
        return 0;
    }
}

int Json::Parser::parse_end_map(void * ctx) {
    try {
        Json * cur = getJson(ctx);
        assert(cur->impl_->type == Json::OBJECT);
        
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            bool cont = s->setDefaults(*cur)
                && s->validRequires(*cur);
            if(!cont) return 0;
        }
        
        setJsonUp(ctx);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception = err.what();
        return 0;
    }
}

int Json::Parser::parse_start_array(void * ctx) {
    try {
        Json * cur = getObj(ctx);
        if( !cur ) return 0;
        assert(cur->impl_->type == Json::UNDEF);
        cur->setData(new Json::Array(), Json::ARRAY);
        // check is array is allowed
        if( getSchema(ctx) ) {
            if( ! getSchema(ctx)->validType(*cur) ) return 0;
        }
        setJson(ctx, cur);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception = err.what();
        return 0;
    }
}

int Json::Parser::parse_end_array(void * ctx) {
    try {
        Json * cur = getJson(ctx);
        assert(cur->impl_->type == Json::ARRAY);
        
        JsonSchema * s = getSchema(ctx);
        if( s ) {
            // check min array length
            if( ! s->validMinSize( *cur ) ) return 0;
        }
        
        setJsonUp(ctx);
        return 1;
    }
    catch( const std::exception & err ) {
        static_cast< Json::Parser::Context *>(ctx)->exception = err.what();
        return 0;
    }
}
    
// yajl_parser_config Json::Parser::cfg = { 1, 1 };
yajl_callbacks Json::Parser::callbacks = {
    Json::Parser::parse_null,
    Json::Parser::parse_boolean,
    NULL,
    NULL,
    Json::Parser::parse_number,
    Json::Parser::parse_string,
    Json::Parser::parse_start_map,
    Json::Parser::parse_map_key,
    Json::Parser::parse_end_map,
    Json::Parser::parse_start_array,
    Json::Parser::parse_end_array
};

} // namespace
Something went wrong with that request. Please try again.