bool FDataTableExporterJSON::WriteStructEntry(const void* InRowData, const UProperty* InProperty, const void* InPropertyData) { const FString Identifier = DataTableUtils::GetPropertyExportName(InProperty, DTExportFlags); if (const UNumericProperty *NumProp = Cast<const UNumericProperty>(InProperty)) { if (NumProp->IsEnum()) { const FString PropertyValue = DataTableUtils::GetPropertyValueAsString(InProperty, (uint8*)InRowData, DTExportFlags); JsonWriter->WriteValue(Identifier, PropertyValue); } else if (NumProp->IsInteger()) { const int64 PropertyValue = NumProp->GetSignedIntPropertyValue(InPropertyData); JsonWriter->WriteValue(Identifier, PropertyValue); } else { const double PropertyValue = NumProp->GetFloatingPointPropertyValue(InPropertyData); JsonWriter->WriteValue(Identifier, PropertyValue); } } else if (const UBoolProperty* BoolProp = Cast<const UBoolProperty>(InProperty)) { const bool PropertyValue = BoolProp->GetPropertyValue(InPropertyData); JsonWriter->WriteValue(Identifier, PropertyValue); } else if (const UArrayProperty* ArrayProp = Cast<const UArrayProperty>(InProperty)) { JsonWriter->WriteArrayStart(Identifier); FScriptArrayHelper ArrayHelper(ArrayProp, InPropertyData); for (int32 ArrayEntryIndex = 0; ArrayEntryIndex < ArrayHelper.Num(); ++ArrayEntryIndex) { const void* ArrayEntryData = ArrayHelper.GetRawPtr(ArrayEntryIndex); WriteArrayEntry(ArrayProp->Inner, ArrayEntryData); } JsonWriter->WriteArrayEnd(); } else if (const UStructProperty* StructProp = Cast<const UStructProperty>(InProperty)) { if (!!(DTExportFlags & EDataTableExportFlags::UseJsonObjectsForStructs)) { JsonWriter->WriteObjectStart(Identifier); WriteStruct(StructProp->Struct, InPropertyData); JsonWriter->WriteObjectEnd(); } else { const FString PropertyValue = DataTableUtils::GetPropertyValueAsString(InProperty, (uint8*)InRowData, DTExportFlags); JsonWriter->WriteValue(Identifier, PropertyValue); } } else { const FString PropertyValue = DataTableUtils::GetPropertyValueAsString(InProperty, (uint8*)InRowData, DTExportFlags); JsonWriter->WriteValue(Identifier, PropertyValue); } return true; }
int main (int argc, char ** argv) { struct MainLevel demo = { (BYTE)0x88, 0xFF, (WORD)0x8844, 0xFF77, (LONG)0x88442211, 0xFF773311, 1.5, 1.75, "Hallo", { (BYTE)0x88, (LONG)0x88442211 }, /* ... */ }; BYTE b = (BYTE)0x88; WORD w = (WORD)0x8844; LONG l = (LONG)0x88442211; FLOAT f = 1.5; DOUBLE d = 1.75; STRPTR s = "Hallo"; struct Level1 l1 = { (BYTE)0x88, (LONG)0x88442211 }; BPTR fh; struct MainLevel * readback; demo.ml_BytePtr = &b; demo.ml_WordPtr = &w; demo.ml_LongPtr = &l; demo.ml_FloatPtr = &f; demo.ml_DoublePtr = &d; demo.ml_StringPtr = &s; demo.ml_Level1Ptr = &l1; fh = Open ("writestruct.dat", MODE_NEWFILE); if (!fh) { PrintFault (IoErr(), "Can't open file\n"); return 10; } /* This writes the following data stream: 0000 88 ml_Byte 0001 ff ml_Ubyte 0002 88 44 ml_Word 0004 ff 77 ml_UWord 0006 88 44 22 11 ml_Long 000a ff 77 33 11 ml_ULong 000e 3f c0 00 00 ml_Float 0012 3f fc 00 00 00 00 00 00 ml_Double 001a 01:48 61 6c 6c 6f 00 ml_String 0021 88 ml_Level1.l1_Byte 0022 88 44 22 11 ml_Level1.l1_Long 0026 01:88 ml_BytePtr 0028 01:88 44 ml_WordPtr 002b 01:88 44 22 11 ml_LongPtr 0030 01:3f c0 00 00 ml_FloatPtr 0035 01:3f fc 00 00 00 00 00 00 ml_DoublePtr 003e 01:01:48 61 6c 6c 6f 00 ml_StringPtr - Note two 01 ! 0046 01:88 88 44 22 11 ml_Level1Ptr */ if (!WriteStruct (&dsh, &demo, fh, MainDesc)) { PrintFault (IoErr(), "Failed to write to file\n"); } if (!Close (fh)) { PrintFault (IoErr(), "Failed to close file\n"); } /* Read the structure back */ fh = Open ("writestruct.dat", MODE_OLDFILE); if (!fh) { PrintFault (IoErr(), "Can't open file for reading\n"); return 10; } if (!ReadStruct (&dsh, (APTR *)&readback, fh, MainDesc)) { PrintFault (IoErr(), "Failed to read from file\n"); } else { UBYTE * ptr; int t; ptr = (UBYTE *)readback; t = 0; kprintf ("readback = %p\n", readback); kprintf ("%02X (88) %02X (FF)\n" , (UBYTE)readback->ml_Byte , readback->ml_UByte ); kprintf ("%04X (8844) %04X (FF77)\n" , (UWORD)readback->ml_Word , readback->ml_UWord ); kprintf ("%08lX (88442211) %08lX (FF773311)\n" , readback->ml_Long , readback->ml_ULong ); kprintf ("%08lX (3FC00000) %08lX:%08lX (3FFC0000:00000000)\n" , *(ULONG *)&readback->ml_Float , ((ULONG *)&readback->ml_Double)[1] , ((ULONG *)&readback->ml_Double)[0] ); kprintf ("%s (Hallo)\n" , readback->ml_String ); kprintf ("{ %02X %08X } ({ 88 88442211 })\n" , (UBYTE)readback->ml_Level1.l1_Byte , readback->ml_Level1.l1_Long ); kprintf ("%02X (88)\n" , (UBYTE)*readback->ml_BytePtr ); kprintf ("%04X (8844)\n" , (UWORD)*readback->ml_WordPtr ); kprintf ("%08lX (88442211)\n" , *readback->ml_LongPtr ); kprintf ("%08lX (3FC00000) %08lX:%08lX (3FFC0000:00000000)\n" , *(ULONG *)readback->ml_FloatPtr , ((ULONG *)readback->ml_DoublePtr)[1] , ((ULONG *)readback->ml_DoublePtr)[0] ); kprintf ("%s (Hallo)\n" , *readback->ml_StringPtr ); kprintf ("{ %02X %08X } ({ 88 88442211 })\n" , (UBYTE)readback->ml_Level1Ptr->l1_Byte , readback->ml_Level1Ptr->l1_Long ); FreeStruct (readback, MainDesc); } if (!Close (fh)) { PrintFault (IoErr(), "Failed to close file after reading\n"); } return 0; } /* main */