LAHumphreys / FixedJSONParserCPP

A faster, typesafe, DOM for RapidJSON

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FixedJSONParserCPP

Build Status Coverage Status

Motivation

FixedJSON

Traditional DOM

Parse JSON Messages into native types:

/*
 * Request the creation of a new order
 *
 * REQUEST:
 *    {
 *        account:      "Account ID",
 *        exchange:     "Exchange ID",
 *        stock:        "Symbol",
 *        type:         "limit",
 *        direction:    "buy",
 *        quantity:     100,
 *        limit_price:  100
 *    }
 */
 // Define the request message
 namespace {
    NewStringField(account);
    NewStringField(direction);
    NewStringField(exchange);
    NewI64Field(limit_price);
    NewI64Field(quantity);
    NewStringField(stock);
    NewStringField(type);
    typedef SimpleParsedJSON<
         account,
         direction,
         exchange,
         limit_price,
         quantity,
         stock,
         type
    > Request;
    Request request;
}

void ReqNewOrder::Setup(const char* req, OrderDefinition& order) {
    std::string error;
    request.Clear();
    if ( !request.Parse(req,error)) {
        throw InvalidRequestException{0,error};
    }

    order.account = request.Get<account>();
    order.direction.SetValue(request.Get<direction>());
    order.venue = request.Get<exchange>();
    order.limitPrice = request.Get<limit_price>();
    order.originalQuantity = request.Get<quantity>();
    order.symbol = request.Get<stock>();
    order.type.SetValue(request.Get<type>());
}

Create JSON Messages from native types:

/*
 *
 * REPLY:
 *    {
 *        fileName:  "gather.out",
 *        units: ["Ir", "Dr"],
 *    }
 *
 *   // The type is defined in the request handler class to be:
 *   NewStringField(fileName);
 *   NewStringArrayField(units);
 *   NewStringArrayField(path);
 *   SimpleParsedJSON<fileName,units,path> reply;
 */
   
    // Build up the reply in the handler...
    std::string ReqGetGraphDetails::OnRequest(const char* JSON) {
        reply.Clear();

        reply.Get<fileName>() = parent->FileName();
        LookupUnits();
        
        // Convert the stored native types to a valid JSON object...
        return reply.GetJSONString();
    }
    
    // Array types are stored in a std::vector...
    void ReqGetGraphDetails::LookupUnits() {
        for ( const size_t& i : NodeConfig::Instance().DisplayIdxs() ) {
            reply.Get<units>().push_back(GetUnit(i));
        }
    }

About

A faster, typesafe, DOM for RapidJSON

License:Apache License 2.0


Languages

Language:C++ 97.4%Language:CMake 2.1%Language:Shell 0.5%