Skip to content

Instantly share code, notes, and snippets.

@mattn
Created July 1, 2009 15:21
Show Gist options
  • Select an option

  • Save mattn/138832 to your computer and use it in GitHub Desktop.

Select an option

Save mattn/138832 to your computer and use it in GitHub Desktop.
#ifndef __JSONL__
#define __JSONL__
#include <string>
#include <vector>
#include <map>
namespace jsonl {
class Array;
class Object;
class Value {
public:
class Null {};
Value() : type_(INVALID_) {}
~Value() {
}
bool parse(std::string::iterator s, std::string::iterator e);
template<typename T> bool is();
template<typename T> T& get();
private:
Value(const Value&);
Value& operator=(const Value&);
enum {
INTEGER_,
STRING_,
BOOL_,
NULL_,
ARRAY_,
OBJECT_,
INVALID_
} type_;
union {
long integer_value_;
std::string* string_value_;
bool bool_value_;
Array* array_value_;
Object* object_value_;
};
};
class Array {
public:
Array();
~Array();
bool parse(std::string::iterator s, std::string::iterator e);
unsigned int size() { return values_.size(); }
template <typename T> T& get(unsigned int i);
private:
Array(const Array&);
Array& operator=(const Array&);
std::vector<Value*> values_;
};
class Object {
public:
Object();
~Object();
bool parse(std::string::iterator s, std::string::iterator e);
std::vector<std::string> keys();
template <typename T> bool has(const std::string& key);
template <typename T> T& get(const std::string& key);
private:
Object(const Object&);
Object& operator=(const Object&);
std::map<std::string, Value*> value_map_;
};
bool Value::parse(std::string::iterator s, std::string::iterator e) {
std::string::iterator it = s;
while(it < e && isspace(*it)) it++;
return false;
}
}
#endif /* __JSONL__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment