summaryrefslogtreecommitdiff
path: root/public/tier1/keyvaluesjson.h
blob: 85cb75f8e5268d4cffdf5f2a868f3256613e2193 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. =================//
//
// Read JSON-formatted data into KeyValues
//
//=============================================================================//

#ifndef KEYVALUESJSON_H
#define KEYVALUESJSON_H

#ifdef _WIN32
#pragma once
#endif

#include "KeyValues.h"

class CUtlBuffer;

/// JSON parser.  Use this class when you need to customize the parsing.
class KeyValuesJSONParser
{
public:
	KeyValuesJSONParser( const CUtlBuffer &buf );
	KeyValuesJSONParser( const char *pszText, int cbSize = -1 );
	~KeyValuesJSONParser();

	/// Parse the whole string.  If there's a problem, returns NULL and sets m_nLine,m_szErrMsg with more info. 
	KeyValues *ParseFile();

	/// Error message is returned here, if there is one.
	char m_szErrMsg[ 1024 ];

	/// Line number of current token during parsing, or of the error, of pasring fails
	int m_nLine;

private:

	bool ParseObject( KeyValues *pObject );
	bool ParseArray( KeyValues *pArray );
	bool ParseValue( KeyValues *pValue );
	void Init( const char *pszText, int cbSize );

	const char *m_cur;
	const char *m_end;

	enum
	{
		kToken_Err = -2, // An error has been discovered, don't parse anything else
		kToken_EOF = -1,
		kToken_String = 1,
		kToken_NumberInt = 2,
		kToken_NumberFloat = 3,
		kToken_True = 4,
		kToken_False = 5,
		kToken_Null = 6,
	};

	int m_eToken;
	CUtlVectorFixedGrowable<char,1024> m_vecTokenChars;

	void NextToken();
	void ParseNumberToken();
	void ParseStringToken();
	const char *GetTokenDebugText();
};

#ifdef _DEBUG
extern void TestKeyValuesJSONParser();
#endif

#endif // KEYVALUESJSON_H