open-source-search-engine/Json.cpp

411 lines
16 KiB
C++
Raw Normal View History

2013-10-12 02:14:26 +04:00
#include "Json.h"
2013-10-16 23:12:22 +04:00
#include "SafeBuf.h"
2013-10-12 02:14:26 +04:00
class JsonItem *Json::addNewItem () {
2013-10-12 03:35:12 +04:00
JsonItem *ji = (JsonItem *)m_sb.getBuf();
if ( m_sb.m_length + (long)sizeof(JsonItem) > m_sb.m_capacity ) {
log("json: preventing buffer breach");
return NULL;
}
// otherwise we got room
2013-10-12 02:14:26 +04:00
m_sb.incrementLength(sizeof(JsonItem));
2013-10-12 03:35:12 +04:00
if ( m_prev ) m_prev->m_next = ji;
ji->m_prev = m_prev;
2013-10-14 23:00:05 +04:00
ji->m_next = NULL;
2013-10-12 03:35:12 +04:00
// we are the new prev now
m_prev = ji;
2013-10-12 02:14:26 +04:00
// value null for now
2013-10-12 03:35:12 +04:00
ji->m_type = JT_NULL;
2013-10-12 02:14:26 +04:00
// parent on stack
JsonItem *parent = NULL;
if ( m_stackPtr > 0 ) parent = m_stack[m_stackPtr-1];
2013-10-12 03:35:12 +04:00
ji->m_parent = parent;
2013-10-12 02:14:26 +04:00
2013-10-12 03:35:12 +04:00
// . if our parent was an array, we are an element in that array
// . if it is an array of objects, then the name will be overwritten
if ( parent ) { // && parent->m_type == JT_ARRAY ) {
2013-10-12 02:14:26 +04:00
// inherit object name from parent
2013-10-12 03:35:12 +04:00
ji->m_name = parent->m_name;
ji->m_nameLen = parent->m_nameLen;
2013-10-12 02:14:26 +04:00
}
2013-10-12 03:35:12 +04:00
return ji;
2013-10-12 02:14:26 +04:00
}
JsonItem *Json::getFirstItem ( ) {
if ( m_sb.length() <= 0 ) return NULL;
return (JsonItem *)m_sb.getBufStart();
}
JsonItem *Json::getItem ( char *name ) {
JsonItem *ji = getFirstItem();
// traverse the json
for ( ; ji ; ji = ji->m_next ) {
// just get STRINGS or NUMS
if ( ji->m_type != JT_STRING && ji->m_type != JT_NUMBER )
continue;
// check name
char *name2 = ji->m_name;
if ( strcmp(name2,name) == 0 ) return ji;
}
return NULL;
}
2013-10-12 02:14:26 +04:00
2013-10-18 05:59:00 +04:00
#include "Mem.h" // gbstrlen()
2013-10-12 03:35:12 +04:00
JsonItem *Json::parseJsonStringIntoJsonItems ( char *json ) {
2013-10-12 02:14:26 +04:00
2013-10-18 05:59:00 +04:00
m_prev = NULL;
2013-10-12 02:14:26 +04:00
m_stackPtr = 0;
2013-10-12 03:35:12 +04:00
m_sb.purge();
JsonItem *ji = NULL;
2013-10-12 02:14:26 +04:00
// how much space will we need to avoid any reallocs?
char *p = json;
2013-10-12 03:35:12 +04:00
bool inQuote = false;
2013-10-12 02:14:26 +04:00
long need = 0;
for ( ; *p ; p++ ) {
// ignore any escaped char. also \x1234
if ( *p == '\\' ) {
if ( p[1] ) p++;
continue;
}
if ( *p == '\"' )
2013-10-12 02:14:26 +04:00
inQuote = ! inQuote;
if ( inQuote )
continue;
2013-10-12 02:14:26 +04:00
if ( *p == '{' ||
*p == ',' ||
*p == '[' ||
*p == ':' )
2013-10-14 23:00:05 +04:00
// +1 for null terminating string of each item
need += sizeof(JsonItem) +1;
2013-10-12 02:14:26 +04:00
}
// plus the length of the string to store it decoded etc.
need += p - json;
2013-10-14 23:00:05 +04:00
// plus a \0 for the value and a \0 for the name of each jsonitem
need += 2;
2013-10-12 02:14:26 +04:00
// this should be enough
2013-10-12 03:35:12 +04:00
if ( ! m_sb.reserve ( need ) ) return NULL;
2013-10-12 02:14:26 +04:00
// for testing if we realloc
2013-10-12 03:35:12 +04:00
char *mem = m_sb.getBufStart();
2013-10-12 02:14:26 +04:00
long size;
2013-10-12 03:35:12 +04:00
char *NAME = NULL;
long NAMELEN = 0;
// reset p
p = json;
2013-10-18 05:59:00 +04:00
// json maybe bad utf8 causing us to miss the \0 char, so use "pend"
char *pend = json + gbstrlen(json);
2013-10-12 03:35:12 +04:00
2013-10-12 02:14:26 +04:00
// scan
2013-10-18 05:59:00 +04:00
for ( ; p < pend ; p += size ) {
2013-10-12 02:14:26 +04:00
// get size
size = getUtf8CharSize ( p );
2013-10-14 23:00:05 +04:00
// skip spaces
if ( is_wspace_a (*p) )
continue;
// skip commas
if ( *p == ',' ) continue;
2013-10-12 02:14:26 +04:00
// did we hit a '{'? that means the existing json item
// is a parent of the item(s) inside the {}'s
if ( *p == '{' ) {
2013-10-12 03:35:12 +04:00
// if ji is non-null it must be a name like in
// \"stats\":{\"fetchTime\":2069,....}
2013-10-12 02:14:26 +04:00
// . this indicates the start of a json object
// . addNewItem() will push the current item on stack
2013-10-12 03:35:12 +04:00
ji = addNewItem();
if ( ! ji ) return NULL;
2013-10-12 02:14:26 +04:00
// current ji is an object type then
2013-10-12 03:35:12 +04:00
ji->m_type = JT_OBJECT;
// set the name
ji->m_name = NAME;
ji->m_nameLen = NAMELEN;
// this goes on the stack
2013-10-14 23:00:05 +04:00
if ( m_stackPtr >= MAXJSONPARENTS ) return NULL;
2013-10-12 03:35:12 +04:00
m_stack[m_stackPtr++] = ji;
// and null this
ji = NULL;
continue;
2013-10-12 02:14:26 +04:00
}
// pop the stack?
if ( *p == '}' ) {
2013-10-12 03:35:12 +04:00
// just pop it and restore name cursor
if ( m_stackPtr > 0 ) {
JsonItem *px = m_stack[m_stackPtr-1];
NAME = px->m_name;
NAMELEN = px->m_nameLen;
m_stackPtr--;
}
continue;
2013-10-12 02:14:26 +04:00
}
// array of things?
if ( *p == '[' ) {
2013-10-12 03:35:12 +04:00
// make a newitem to put on stack
ji = addNewItem();
if ( ! ji ) return NULL;
// current ji is an object type then
ji->m_type = JT_ARRAY;
// set the name
ji->m_name = NAME;
ji->m_nameLen = NAMELEN;
// this goes on the stack
2013-10-14 23:00:05 +04:00
if ( m_stackPtr >= MAXJSONPARENTS ) return NULL;
2013-10-12 03:35:12 +04:00
m_stack[m_stackPtr++] = ji;
ji = NULL;
continue;
}
// pop the stack?
if ( *p == ']' ) {
// just pop it and restore name cursor
if ( m_stackPtr > 0 ) {
JsonItem *px = m_stack[m_stackPtr-1];
NAME = px->m_name;
NAMELEN = px->m_nameLen;
m_stackPtr--;
}
continue;
2013-10-12 02:14:26 +04:00
}
// a quote?
if ( *p == '\"' ) {
// find end of quote
char *end = p + 1;
2013-11-19 21:44:42 +04:00
for ( ; *end ; end++ ) {
// skip two chars if escaped
if ( *end == '\\' && end[1] ) {
end++;
continue;
}
// this quote is unescaped then
if ( *end == '\"' ) break;
}
2013-10-12 02:14:26 +04:00
// field?
char *x = end + 1;
// skip spaces
for ( ; *x && is_wspace_a(*x) ; x++ );
// define the string
char *str = p + 1;
long slen = end - str;
// . if a colon follows, it was a field
if ( *x == ':' ) {
2013-10-14 23:00:05 +04:00
// let's push this now so we can \0 term
char *savedStr = m_sb.getBuf();
m_sb.safeMemcpy ( str , slen );
m_sb.pushChar('\0');
2013-10-12 03:35:12 +04:00
// just set the name cursor
2013-10-14 23:00:05 +04:00
NAME = savedStr;//str;
2013-10-12 03:35:12 +04:00
NAMELEN = slen;
2013-10-12 02:14:26 +04:00
}
// . otherwise, it was field value, so index it
// . TODO: later make field names compounded to
// better represent nesting?
else {
2013-10-12 03:35:12 +04:00
// make a new one in safebuf. our
// parent will be the array type item.
ji = addNewItem();
2013-10-14 23:00:05 +04:00
if ( ! ji ) return NULL;
2013-10-12 02:14:26 +04:00
// we are a string
2013-10-12 03:35:12 +04:00
ji->m_type = JT_STRING;
// use name cursor
ji->m_name = NAME;
ji->m_nameLen = NAMELEN;
2013-10-12 02:14:26 +04:00
// get length decoded
2013-10-12 03:35:12 +04:00
long curr = m_sb.length();
2013-10-12 02:14:26 +04:00
// store decoded string right after jsonitem
2013-10-12 03:35:12 +04:00
if ( !m_sb.safeDecodeJSONToUtf8 ( str, slen,0))
2013-10-12 02:14:26 +04:00
return NULL;
// store length decoded json
2013-10-12 03:35:12 +04:00
ji->m_valueLen = m_sb.length() - curr;
2013-10-14 23:00:05 +04:00
// end with a \0
m_sb.pushChar('\0');
2013-10-12 03:35:12 +04:00
// ok, this one is done
ji = NULL;
2013-10-12 02:14:26 +04:00
}
// skip over the string
2013-10-12 03:35:12 +04:00
size = 0;
2013-10-12 02:14:26 +04:00
p = x;
continue;
}
2013-10-14 23:00:05 +04:00
// true or false?
if ( (*p == 't' && strncmp(p,"true",4)==0) ||
(*p == 'f' && strncmp(p,"false",5)==0) ) {
// make a new one
ji = addNewItem();
if ( ! ji ) return NULL;
// copy the number as a string as well
long curr = m_sb.length();
// what is the length of it?
long slen = 4;
ji->m_valueLong = 1;
ji->m_valueDouble = 1.0;
if ( *p == 'f' ) {
slen = 5;
ji->m_valueLong = 0;
ji->m_valueDouble = 0;
}
// store decoded string right after jsonitem
if ( !m_sb.safeDecodeJSONToUtf8 (p,slen,0))
return NULL;
// store length decoded json
ji->m_valueLen = m_sb.length() - curr;
// end with a \0
m_sb.pushChar('\0');
ji->m_type = JT_NUMBER;
// use name cursor
ji->m_name = NAME;
ji->m_nameLen = NAMELEN;
ji = NULL;
// skip over the string
size = 1;
//p = end;
2013-10-14 23:00:05 +04:00
continue;
}
2013-10-12 02:14:26 +04:00
// if we hit a digit they might not be in quotes like
// "crawled":123
2013-10-12 03:35:12 +04:00
if ( is_digit ( *p ) ||
// like .123 ?
( *p == '.' && is_digit(p[1]) ) ) {
2013-10-12 02:14:26 +04:00
// find end of the number
char *end = p + 1;
2013-10-14 23:00:05 +04:00
// . allow '.' for decimal numbers
// . TODO: allow E for exponent
for ( ; *end && (is_digit(*end) || *end=='.');end++) ;
2013-10-12 02:14:26 +04:00
// define the string
2013-10-12 03:35:12 +04:00
char *str = p;
2013-10-14 23:00:05 +04:00
long slen = end - str;
2013-10-12 03:35:12 +04:00
// make a new one
ji = addNewItem();
if ( ! ji ) return NULL;
2013-10-12 02:14:26 +04:00
// decode
2013-10-12 03:35:12 +04:00
//char c = str[slen];
//str[slen] = '\0';
ji->m_valueLong = atol(str);
ji->m_valueDouble = atof(str);
2013-10-14 23:00:05 +04:00
// copy the number as a string as well
long curr = m_sb.length();
// store decoded string right after jsonitem
if ( !m_sb.safeDecodeJSONToUtf8 ( str, slen,0))
return NULL;
// store length decoded json
ji->m_valueLen = m_sb.length() - curr;
// end with a \0
m_sb.pushChar('\0');
2013-10-12 03:35:12 +04:00
//str[slen] = c;
ji->m_type = JT_NUMBER;
// use name cursor
ji->m_name = NAME;
ji->m_nameLen = NAMELEN;
ji = NULL;
2013-10-12 02:14:26 +04:00
// skip over the string
2013-10-12 03:35:12 +04:00
size = 0;
2013-10-12 02:14:26 +04:00
p = end;
continue;
}
}
// for testing if we realloc
2013-10-12 03:35:12 +04:00
char *memEnd = m_sb.getBufStart();
2013-10-12 02:14:26 +04:00
if ( mem != memEnd ) { char *xx=NULL;*xx=0; }
2013-10-12 03:35:12 +04:00
return (JsonItem *)m_sb.getBufStart();
2013-10-12 02:14:26 +04:00
}
void Json::test ( ) {
char *json = "{\"tags\":[\"Apple Inc.\",\"Symbian\",\"IPad\",\"Music\"],\"summary\":\"Good timing and shrewd planning have played as much of a role as innovative thinking for the Silicon Valley juggernaut.\",\"icon\":\"http://www.onlinemba.com/wp-content/themes/onlinemba/assets/img/ico/apple-touch-icon.png\",\"text\":\"How did Apple rise through the ranks to become the worlds most profitable tech company? As it turns out, good timing and shrewd planning have played as much of a role as innovative thinking for the Silicon Valley juggernaut.For example, take the first MP3 player — MPMan, produced by South Korea-based SaeHan Information Systems. MPMan appeared in 1998, three years before the first iPods were released. As the original pioneer of portable MP3 player technology, SaeHan spent a good deal of time in court negotiating terms of use with various record companies. By 2001, a clear legal precedent was set for MP3 access — allowing Apple to focus less on courtroom proceedings and more on cutting-edge marketing campaigns for their new product."
"When all else fails, they buy it: While iPads had fan boys salivating in the streets the technology has been around for decades. One of the most obvious precursors to the iPad is FingerWorks, a finger gesture operated keyboard with a mouse very similar to Apples iPad controller. Fingerworks was bought in 2005 by none other than Apple not surprisingly a couple years before the release of the iPhone and later the iPad. Of course, this isnt to say that Apple doesnt deserve to be the most valuable tech company in the world just that innovation isnt always about being first or best, sometimes, its just perception.\",\"stats\":{\"fetchTime\":2069,\"confidence\":\"0.780\"},\"type\":\"article\",\"meta\":{\"twitter\":{\"twitter:creator\":\"@germanny\",\"twitter:domain\":\"OnlineMBA.com\",\"twitter:card\":\"summary\",\"twitter:site\":\"@OnlineMBA_com\"},\"microdata\":{\"itemprop:image\":\"http://www.onlinemba.com/wp-content/uploads/2013/02/apple-innovates-featured-150x150.png\"},\"title\":\"3 Ways Apple Actually Innovates - OnlineMBA.com\",\"article:publisher\":\"https://www.facebook.com/OnlineMBAcom\",\"fb:app_id\":\"274667389269609\",\"og\":{\"og:type\":\"article\",\"og:title\":\"3 Ways Apple Actually Innovates - OnlineMBA.com\",\"og:description\":\"Good timing and shrewd planning have played as much of a role as innovative thinking for the Silicon Valley juggernaut.\",\"og:site_name\":\"OnlineMBA.com\",\"og:image\":\"http://www.onlinemba.com/wp-content/uploads/2013/02/apple-innovates-featured-150x150.png\",\"og:locale\":\"en_US\",\"og:url\":\"http://www.onlinemba.com/blog/3-ways-apple-innovates\"}},\"human_language\":\"en\",\"url\":\"http://www.onlinemba.com/blog/3-ways-apple-innovates\",\"title\":\"3 Ways Apple Actually Innovates\",\"textAnalysis\":{\"error\":\"Timeout during text analysis\"},\"html\":\"<div><div class=\\\"image_frame\\\"><img data-blend-adjustment=\\\"http://www.onlinemba.com/wp-content/themes/onlinemba/assets/img/backgrounds/bg.gif\\\" data-blend-mode=\\\"screen\\\" src=\\\"http://www.onlinemba.com/wp-content/uploads/2013/02/apple-innovates-invert-350x350.png\\\"></img></div><p>How did Apple rise"
"\",\"supertags\":[{\"id\":856,\"positions\":[[7,12],[41,46],[663,668],[776,781],[1188,1193],[1380,1385],[1645,1650],[1841,1848],[2578,2583],[2856,2863],[2931,2936]],\"name\":\"Apple Inc.\",\"score\":0.8,\"contentMatch\":1,\"categories\":{\"1752615\":\"Home computer hardware companies\",\"27841529\":\"Technology companies of the United States\",\"33847259\":\"Publicly traded companies of the United States\",\"15168154\":\"Mobile phone manufacturers\",\"732736\":\"Retail companies of the United States\",\"9300270\":\"Apple Inc.\",\"23568549\":\"Companies based in Cupertino, "
"California\",\"34056227\":\"Article Feedback 5\",\"37595560\":\"1976 establishments in California\",\"7415072\":\"Networking hardware companies\",\"699547\":\"Computer hardware companies\",\"37191508\":\"Software companies based in the San Francisco Bay Area\",\"855278\":\"Electronics companies\",\"5800057\":\"Steve Jobs\",\"7652766\":\"Display technology companies\",\"14698378\":\"Warrants issued in Hong Kong Stock Exchange\",\"4478067\":\"Portable audio player manufacturers\",\"31628257\":\"Multinational companies headquartered in the United States\",\"732825\":\"Electronics companies of the United States\",\"733759\":\"Computer companies of the United States\",\"6307421\":\"Companies established in 1976\"},\"type\":1,\"senseRank\":1,\"variety\":0.21886792452830184,\"depth\":0.6470588235294117},{\"id\":25686223,\"positions\":[[895,902],[2318,2325]],\"name\":\"Symbian\",\"score\":"
"0.8,\"contentMatch\":0.9162303664921466,\"categories\":{\"33866248\":\"Nokia platforms\",\"20290726\":\"Microkernel-based operating systems\",\"39774425\":\"ARM operating systems\",\"2148723\":\"Real-time operating systems\",\"953043\":\"Smartphones\",\"10817505\":\"History of software\",\"17862682\":\"Mobile phone operating systems\",\"33569166\":\"Accenture\",\"2150815\":\"Embedded operating systems\",\"22533699\":\"Symbian OS\",\"22280474\":\"Mobile operating systems\"},\"type\":1,\"senseRank\":1,\"variety\":0.6566037735849057,\"depth\":0.6470588235294117},{\"id\":25970423,\"positions\":[[2639,2644],[2771,2775],[2864,2868]],\"name\":\"IPad\",\"score\":0.8,\"contentMatch\":1,\"categories\":{\"33578068\":\"Products introduced "
"in 2010\",\"18083009\":\"Apple personal digital assistants\",\"23475157\":\"Touchscreen portable media players\",\"30107877\":\"IPad\",\"9301031\":\"Apple Inc. hardware\",\"27765345\":\"IOS (Apple)\",\"26588084\":\"Tablet computers\"},\"type\":1,\"senseRank\":1,\"variety\":0.49056603773584906,\"depth\":0.5882352941176471},{\"id\":18839,\"positions\":[[1945,1950],[2204,2209]],\"name\":\"Music\",\"score\":0.7,\"contentMatch\":1,\"categories\":{\"991222\":\"Performing arts\",\"693016\":\"Entertainment\",\"691484\":\"Music\"},\"type\":1,\"senseRank\":1,\"variety\":0.22264150943396221,\"depth\":0.7058823529411764}],\"media\":[{\"pixelHeight\":350,\"link\":\"http://www.onlinemba.com/wp-content/uploads/2013/02/apple-innovates-invert-350x350.png\",\"primary\":\"true\",\"pixelWidth\":350,\"type\":\"image\"}]}";
2013-10-12 03:35:12 +04:00
JsonItem *ji = parseJsonStringIntoJsonItems ( json );
2013-10-12 02:14:26 +04:00
// print them out?
log("json: type0=%li",(long)ji->m_type);
return;
}
bool JsonItem::getCompoundName ( SafeBuf &nameBuf ) {
// reset, but don't free mem etc. just set m_length to 0
nameBuf.reset();
// get its full compound name like "meta.twitter.title"
JsonItem *p = this;//ji;
char *lastName = NULL;
char *nameArray[20];
long numNames = 0;
for ( ; p ; p = p->m_parent ) {
// empty name?
if ( ! p->m_name ) continue;
if ( ! p->m_name[0] ) continue;
// dup? can happen with arrays. parent of string
// in object, has same name as his parent, the
// name of the array. "dupname":[{"a":"b"},{"c":"d"}]
if ( p->m_name == lastName ) continue;
// update
lastName = p->m_name;
// add it up
nameArray[numNames++] = p->m_name;
// breach?
if ( numNames < 15 ) continue;
log("build: too many names in json tag");
break;
}
// assemble the names in reverse order which is correct order
for ( long i = 1 ; i <= numNames ; i++ ) {
// copy into our safebuf
if ( ! nameBuf.safeStrcpy ( nameArray[numNames-i]) )
return false;
// separate names with periods
if ( ! nameBuf.pushChar('.') ) return false;
}
// remove last period
nameBuf.removeLastChar('.');
// and null terminate
if ( ! nameBuf.nullTerm() ) return false;
// change all :'s in names to .'s since : is reserved!
char *px = nameBuf.getBufStart();
for ( ; *px ; px++ ) if ( *px == ':' ) *px = '.';
return true;
}
// is this json item in an array of json items?
bool JsonItem::isInArray ( ) {
JsonItem *p = this;//ji;
for ( ; p ; p = p->m_parent ) {
// empty name? it's just a "value item" then, i guess.
//if ( ! p->m_name ) continue;
//if ( ! p->m_name[0] ) continue;
if ( p->m_type == JT_ARRAY ) return true;
}
return false;
}