Пример #1
0
VError VJSONBinaryWriter::StreamArray(const VJSONArray *inArray, VMemoryBuffer<>& outBuff)
{
	VError err = VE_OK;
	
	if (inArray == NULL) // should it happen
	{
		sLONG nullarray = -2;
		outBuff.AddData(&nullarray, 4);
		return VE_OK;
	}
	
	else
	{
		sLONG count = (sLONG)inArray->GetCount();
		sLONG xcount = count; // so count can be optimized as a register
		outBuff.AddData(&xcount, 4);

		for( sLONG i = 0 ; (i < count) && (err == VE_OK) ; ++i)
		{
			err = StreamValue( (*inArray)[i], outBuff);
		}

	}
		
	return err;
}
Пример #2
0
VError VJSONBinaryWriter::StreamValue(const VJSONValue& inValue, VMemoryBuffer<>& outBuff)
{
	VError err = VE_OK;
	sBYTE btype = (sBYTE)inValue.GetType();
	outBuff.AddData(&btype, 1);
	switch( inValue.GetType())
	{
		case JSON_null:
		case JSON_undefined:
		case JSON_true:
		case JSON_false:
			{
				break;
			}
			
		case JSON_string:
			{
				const VInlineString* s = &(inValue.fString);
				sLONG len = (sLONG)s->GetLength();
				outBuff.AddData(&len, 4);
				if (len > 0)
					outBuff.AddData(s->GetCPointer(), len * 2);
				break;
			}

		case JSON_date:
			{
				outBuff.AddData(&inValue.fTimeStamp, sizeof(sLONG8));
				break;
			}

		case JSON_number:
			{
				outBuff.AddData(&inValue.fNumber, sizeof(Real));
				break;
			}

		case JSON_array:
			{
				err = StreamArray( inValue.GetArray(), outBuff);
				break;
			}

		case JSON_object:
			{
				err = StreamObject( inValue.GetObject(), outBuff);
				break;
			}
		
		default:
			xbox_assert( false);
	}
	
	return err;
}
Пример #3
0
VError VJSONBinaryWriter::StreamObject(const VJSONObject *inObject, VMemoryBuffer<>& outBuff)
{
	if (inObject == NULL) // should it happen
	{
		sWORD nullobj = -2;
		outBuff.AddData(&nullobj, 2);
		return VE_OK;
	}

	if (std::find( fStack.begin(), fStack.end(), inObject) != fStack.end())
	{
		return vThrowError( VE_JSON_STRINGIFY_CIRCULAR);
	}

	VError err = VE_OK;

	fStack.push_back( inObject);
	
	//if (!inObject->doStream( outBuff, *this, &err))
	{
		for (VJSONPropertyConstOrderedIterator i(inObject); i.IsValid() && (err == VE_OK); ++i)
		{
			const VString& name = i.GetName();
			sWORD len = (sWORD)name.GetLength();
			outBuff.AddData(&len, 2);
			if (len > 0)
			{
				outBuff.AddData(name.GetCPointer(), (sLONG)len * 2);
				err = StreamValue(i.GetValue(), outBuff);
			}
		}

		sWORD endobj = -1;
		outBuff.AddData(&endobj, 2);
	}

	fStack.pop_back();
	
	return err;
}