summaryrefslogtreecommitdiff
path: root/db/json.cpp
blob: 6c875ab95d4095429b7f207164ea838224fcfd26 (plain)
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
// json.cpp

#include "stdafx.h"
#include "json.h"
#include "../util/builder.h"

/* partial implementation for now */

void skipWhite(const char *&p) { 
	while( *p == ' ' || *p == '\r' || *p == '\n' || *p == '\t' )
		p++;
}

void value(JSObjBuilder& b, const char *&p, string& id) { 
	if( strncmp(p, "ObjId()", 7) == 0 ) {
		p += 7;
		b.appendOID(id.c_str());
	}
}

void _fromjson(JSObjBuilder& b, const char *&p) { 
	while( 1 ) { 
		skipWhite(p);
		if( *p == 0 )
			break;
		if( *p == '{' ) { _fromjson(b,++p); continue; }
		if( *p == '}' ) { ++p; break; }
		if( *p == '_' || isalpha(*p) ) { 
			string id;
			while( *p == '_' || isalpha(*p) || isdigit(*p)  ) { 
				id += *p++;
			}
			skipWhite(p);
			assert( *p == ':' ); p++;
			skipWhite(p);
			value(b, p, id);
			continue;
		}
	}
}

JSObj fromjson(const char *str) { 
	JSObjBuilder b;
	_fromjson(b,str);
	return b.doneAndDecouple();
}