Пример #1
0
//---------------------------------------------------------
bool CSG_MetaData::from_JSON(const CSG_String &JSON)
{
	Destroy();

	Set_Name("root");

	CSG_MetaData	*pNode	= this;

	const SG_Char	*pc	= JSON.c_str();

	while( *pc )
	{
		CSG_String	Element;

		for(bool bQuota=false;;)
		{
			SG_Char c = *pc++;
			
			if( !c || c == '\n' ) { break; } else
			{
				if( c == '\"' )
				{
					Element += c; bQuota = !bQuota;
				}
				else if( bQuota || (c != ' ' && c != '\t' && c != ',') )
				{
					Element += c;
				}
			}
		}

		//-------------------------------------------------
		if( Element.is_Empty() )
		{
			// nop
		}
		else if( Element.Find('[') >= 0 )	// array begins
		{
			pNode	= pNode->Add_Child(Element.AfterFirst('\"').BeforeFirst('\"'));

			pNode->Add_Property("array", 1);
		}
		else if( Element.Find(']') >= 0 )	// array ends
		{
			if( pNode != this )
			{
				pNode	= pNode->Get_Parent();
			}
		}
		else if( Element.Find('{') >= 0 )	// object begins
		{
			Element	= Element.AfterFirst('\"').BeforeFirst('\"');

			if( !Element.is_Empty() )
			{
				pNode	= pNode->Add_Child(Element);
			}
			else if( pNode->Get_Property("array") )
			{
				pNode	= pNode->Add_Child(CSG_String::Format("%d", pNode->Get_Children_Count()));
			}
		}
		else if( Element.Find('}') >= 0 )	// object ends
		{
			if( pNode != this )
			{
				pNode	= pNode->Get_Parent();
			}
		}
		else
		{
			CSG_String	Key  (Element.AfterFirst('\"').BeforeFirst('\"'));
			CSG_String	Value(Element.AfterFirst(':'));

			if( Value.Find('\"') > -1 )
			{
				Value	= Value.AfterFirst('\"').BeforeFirst('\"');
			}

			pNode->Add_Child(Key, Value);
		}
	}

	return( true );
}