Ejemplo n.º 1
0
/**
* Creates a JSON encoded string for the value with all necessary characters escaped
*
* @access private
*
* @param size_t indentDepth The prettyprint indentation depth (0 : no prettyprint)
*
* @return String Returns the JSON string
*/
String JSONValue::stringifyImpl(size_t const indentDepth) const {
	String ret_string;
	size_t const indentDepth1 = indentDepth ? indentDepth + 1 : 0;
	String const indentStr = indent(indentDepth);
	String const indentStr1 = indent(indentDepth1);

	switch (_type) {
	case JSONType_Null:
		ret_string = "null";
		break;

	case JSONType_String:
		ret_string = stringifyString(*_stringValue);
		break;

	case JSONType_Bool:
		ret_string = _boolValue ? "true" : "false";
		break;

	case JSONType_Number: {
		if (isinf(_numberValue) || isnan(_numberValue))
			ret_string = "null";
		else {
			char str[80];
			sprintf(str, "%lg", _numberValue);
			ret_string = str;
		}
		break;
	}

	case JSONType_IntegerNumber: {
		char str[80];
		sprintf(str, "%lld", _integerValue);
		ret_string = str;
		break;
	}

	case JSONType_Array: {
		ret_string = indentDepth ? "[\n" + indentStr1 : "[";
		JSONArray::const_iterator iter = _arrayValue->begin();
		while (iter != _arrayValue->end()) {
			ret_string += (*iter)->stringifyImpl(indentDepth1);

			// Not at the end - add a separator
			if (++iter != _arrayValue->end())
				ret_string += ",";
		}
		ret_string += indentDepth ? "\n" + indentStr + "]" : "]";
		break;
	}

	case JSONType_Object: {
		ret_string = indentDepth ? "{\n" + indentStr1 : "{";
		JSONObject::const_iterator iter = _objectValue->begin();
		while (iter != _objectValue->end()) {
			ret_string += stringifyString((*iter)._key);
			ret_string += ":";
			ret_string += (*iter)._value->stringifyImpl(indentDepth1);

			// Not at the end - add a separator
			if (++iter != _objectValue->end())
				ret_string += ",";
		}
		ret_string += indentDepth ? "\n" + indentStr + "}" : "}";
		break;
	}
	}

	return ret_string;
}
Ejemplo n.º 2
0
Archivo: JSON.cpp Proyecto: cefix/cefix
/**
 * Turns the passed in JSONValue into a JSON encode string
 *
 * @access public
 *
 * @param JSONValue* value The root value
 *
 * @return std::wstring Returns a JSON encoded string representation of the given value
 */
std::string JSON::stringifyToUTF8(const std::string& key, AbstractProperty *value, unsigned int depth)
{
	if (value == NULL)
		return "";
	std::string type_str = value->getXmlType();
	
	std::ostringstream ss;
	if (depth)
		ss << std::setw(depth * 2) << " ";
		
	if (!key.empty())
		ss << stringifyString(key) << ": ";
	
	switch (value->getType()) {

		case AbstractProperty::UNDEFINED:	
			ss << "null";
			break;
				
		case AbstractProperty::INTEGER:
			if (type_str == "boolean") {
				ss << ((value->asInt() != 0) ? "true" : "false");
			} else {
				ss << value->asString();
			}
			break;
			
		case AbstractProperty::FLOAT:
		case AbstractProperty::DOUBLE:
			ss << value->asString();
			break;
			
		case AbstractProperty::STRING:
			ss << stringifyString(value->asString());
			break;
			
		case AbstractProperty::VEC2F:
		case AbstractProperty::VEC2D:
			{
				osg::Vec2d v = value->asVec2d();
				if (type_str == "size") 
					ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"width\": " << v[0] << ", \"height\": " << v[1] << " } ";
				else
					ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"x\": " << v[0] << ", \"y\": " << v[1] << " } ";
			}
			break;
			
		case AbstractProperty::VEC3F:
		case AbstractProperty::VEC3D:
			{
				osg::Vec3d v = value->asVec3d();
				ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"x\": " << v[0] << ", \"y\": " << v[1] << ", \"z\": " << v[2] << " } ";
			}
			break;
			
		case AbstractProperty::VEC4F:
		case AbstractProperty::VEC4D:
			{
				osg::Vec4d v = value->asVec4d();
				if (type_str == "color")
					ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"r\": " << v[0] << ", \"g\": " << v[1] << ", \"b\": " << v[2] << ", \"a\": " << v[3] << " } ";
				else if (type_str == "rect")
					ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"l\": " << v[0] << ", \"t\": " << v[1] << ", \"w\": " << (v[2]-v[0]) << ", \"h\": " << (v[3]-v[1]) << " } ";
				else 
					ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"x\": " << v[0] << ", \"y\": " << v[1] << ", \"z\": " << v[2] << ", \"w\": " << v[3] << " } ";

			}
			break;
		
		case AbstractProperty::MATRIXF:
		case AbstractProperty::MATRIXD:
			{
				osg::Matrixd v = value->asMatrixd();
				ss << "{ \"" << s_type_key << "\": " << stringifyString(type_str) << ", ";
				for(unsigned int y=0; y<4; ++y) {
					for(unsigned int x=0; x<4; ++x) {
						ss << " \"i"+intToString(x)+intToString(y) << "\": " << floatToString(v(x,y));
						if ((y == 3) && (x == 3)) {
							;
						} 
						else {
							ss << ", ";
						}
					}
				}
				ss << " } ";
			}
			break;
			
		case AbstractProperty::QUAT:
			{
				osg::Quat v = value->asQuat();
				ss << "{ \"" << s_type_key << "\": \"" << type_str << "\", \"x\": " << v[0] << ", \"y\": " << v[1] << ", \"z\": " << v[2] << ", \"w\": " << v[3] << " } ";
			}
			break;

		case AbstractProperty::PROPERTY_LIST:
			{
				cefix::PropertyList* pl = value->asPropertyList();
				if (!pl)
					ss << "null\n";
				else 
				{
					ss << " { \n";
					unsigned j(0);
					for (PropertyList::PropertyListT::iterator i = pl->_list.begin(); i != pl->_list.end(); ++i, ++j) 
					{
						ss << stringifyToUTF8(i->first, i->second.get(), depth + 1);
						if (j+1 != pl->_list.size()) 
							ss << ", \n";
					}
					ss << "\n";
					if (depth)
						ss << std::setw(depth * 2) << " " << "}";
				}
			}
			break;
		
		case AbstractProperty::MULTIPLE_VALUE_LIST:
			{
				MultipleValueList::List& l = value->asMultipleValueList()->list();
				
				ss << " [ \n";
				unsigned int j(0);
				for(MultipleValueList::List::iterator i = l.begin(); i != l.end(); ++i, ++j) 
				{
					ss << stringifyToUTF8("", (*i), depth + 1);
					if (j+1 != l.size())
						ss << ", \n";
				}
				
				ss << "\n";
				
                if (depth)
					ss << std::setw(depth * 2) << " "; 
                ss << "]";
			}
			break;
			
		case AbstractProperty::OBJECT:
			ss << "null";
			break;
		default:
			break;
	}
	
	return ss.str();
}