Ejemplo n.º 1
0
void VJSONValue::test()
{
    const char *sTestJson = "{\"good\":true,\"name\":\"smith\",\"paul\":{\"good\":false,\"name\":\"paul\",\"Nul\":null,\"age\":10.321,\"text\":\"\"\\/\b\f\n\r\t\"},\"age\":42}";

    VJSONValue	object( JSON_object);

    object.SetProperty( CVSTR( "name"), VJSONValue( CVSTR( "smith")));
    object.SetProperty( CVSTR( "age"), VJSONValue( 42));
    object.SetProperty( CVSTR( "good"), VJSONValue::sTrue);

    for( VJSONPropertyIterator i( object.GetObject()) ; i.IsValid() ; ++i)
    {
        VString propertyName = i.GetName();
        VJSONValue propertyValue = i.GetValue();
    }

    VJSONValue	object_child( JSON_object);

    object_child.SetProperty( CVSTR( "name"), VJSONValue( CVSTR( "paul")));
    object_child.SetProperty( CVSTR( "age"), VJSONValue( 10.321));
    object_child.SetProperty( CVSTR( "good"), VJSONValue::sFalse);
    object_child.SetProperty( CVSTR( "text"), VJSONValue( CVSTR( "\"\\/\b\f\n\r\t")));
    object_child.SetProperty( CVSTR( "Nul"), VJSONValue::sNull);
    object_child.SetProperty( CVSTR( "undef"), VJSONValue::sUndefined);

    VJSONArray		*object_children = new VJSONArray;
    object_children->Push( object_child);

    object.SetProperty( CVSTR( "children"), VJSONValue( object_children));

    ReleaseRefCountable( &object_children);

    VString s;
    VJSONWriter writer( JSON_WithQuotesIfNecessary);
    VError err = writer.StringifyValue( object, s);

    DebugMsg( s);
    DebugMsg( "\n");

    VJSONWriter writer2( JSON_WithQuotesIfNecessary | JSON_PrettyFormatting);
    err = writer2.StringifyValue( object, s);

    DebugMsg( s);
    DebugMsg( "\n");

    VJSONValue value;
    VJSONImporter importer(s);
    err = importer.Parse( value);
    VString s2;
    writer2.StringifyValue( value, s2);
    DebugMsg( s2);
    DebugMsg( "\n");
    xbox_assert( s == s2);

    {
        VJSONImporter importer2( CVSTR( "[1,2,3,[4,5],6]"));
        err = importer2.Parse( value);
        value.GetString( s2);
        DebugMsg( s2);
        DebugMsg( "\n");
    }

}
Ejemplo n.º 2
0
void TestSerialize(T* pObject)
{
  ezAbstractObjectGraph graph;
  TestContext context;
  ezRttiConverterWriter conv(&graph, &context, true, true);

  const ezRTTI* pRtti = ezGetStaticRTTI<T>();
  ezUuid guid;
  guid.CreateNewUuid();

  context.RegisterObject(guid, pRtti, pObject);
  ezAbstractObjectNode* pNode = conv.AddObjectToGraph(pRtti, pObject, "root");

  EZ_TEST_BOOL(pNode->GetGuid() == guid);
  EZ_TEST_STRING(pNode->GetType(), pRtti->GetTypeName());
  EZ_TEST_INT(pNode->GetProperties().GetCount(), pNode->GetProperties().GetCount());

  {
    ezMemoryStreamStorage storage;
    ezMemoryStreamWriter writer(&storage);
    ezMemoryStreamReader reader(&storage);

    ezAbstractGraphDdlSerializer::Write(writer, &graph);

    ezStringBuilder sData, sData2;
    sData.SetSubString_ElementCount((const char*)storage.GetData(), storage.GetStorageSize());


    ezRttiConverterReader convRead(&graph, &context);
    auto* pRootNode = graph.GetNodeByName("root");
    EZ_TEST_BOOL(pRootNode != nullptr);

    T target;
    convRead.ApplyPropertiesToObject(pRootNode, pRtti, &target);
    EZ_TEST_BOOL(target == *pObject);

    // Overwrite again to test for leaks as existing values have to be removed first by ezRttiConverterReader.
    convRead.ApplyPropertiesToObject(pRootNode, pRtti, &target);
    EZ_TEST_BOOL(target == *pObject);

    {
      T clone;
      ezReflectionSerializer::Clone(pObject, &clone, pRtti);
      EZ_TEST_BOOL(clone == *pObject);
      EZ_TEST_BOOL(ezReflectionUtils::IsEqual(&clone, pObject, pRtti));
    }

    {
      T* pClone = ezReflectionSerializer::Clone(pObject);
      EZ_TEST_BOOL(*pClone == *pObject);
      EZ_TEST_BOOL(ezReflectionUtils::IsEqual(pClone, pObject));
      // Overwrite again to test for leaks as existing values have to be removed first by clone.
      ezReflectionSerializer::Clone(pObject, pClone, pRtti);
      EZ_TEST_BOOL(*pClone == *pObject);
      EZ_TEST_BOOL(ezReflectionUtils::IsEqual(pClone, pObject, pRtti));
      pRtti->GetAllocator()->Deallocate(pClone);
    }

    ezAbstractObjectGraph graph2;
    ezAbstractGraphDdlSerializer::Read(reader, &graph2);

    ezMemoryStreamStorage storage2;
    ezMemoryStreamWriter writer2(&storage2);

    ezAbstractGraphDdlSerializer::Write(writer2, &graph2);
    sData2.SetSubString_ElementCount((const char*)storage2.GetData(), storage2.GetStorageSize());

    EZ_TEST_BOOL(sData == sData2);
  }

  {
    ezMemoryStreamStorage storage;
    ezMemoryStreamWriter writer(&storage);
    ezMemoryStreamReader reader(&storage);

    ezAbstractGraphBinarySerializer::Write(writer, &graph);

    ezRttiConverterReader convRead(&graph, &context);
    auto* pRootNode = graph.GetNodeByName("root");
    EZ_TEST_BOOL(pRootNode != nullptr);

    T target;
    convRead.ApplyPropertiesToObject(pRootNode, pRtti, &target);
    EZ_TEST_BOOL(target == *pObject);

    ezAbstractObjectGraph graph2;
    ezAbstractGraphBinarySerializer::Read(reader, &graph2);

    ezMemoryStreamStorage storage2;
    ezMemoryStreamWriter writer2(&storage2);

    ezAbstractGraphBinarySerializer::Write(writer2, &graph2);

    EZ_TEST_INT(storage.GetStorageSize(), storage2.GetStorageSize());

    if (storage.GetStorageSize() == storage2.GetStorageSize())
    {
      EZ_TEST_BOOL(ezMemoryUtils::ByteCompare<ezUInt8>(storage.GetData(), storage2.GetData(), storage.GetStorageSize()) == 0);
    }
  }
}