@@ -452,21 +452,47 @@ struct GffStruct {
452
452
this (in nwnlibd.orderedjson.JSONValue json){
453
453
enforce(json.type == JSONType.object, " json value " ~ json.toPrettyString ~ " is not an object" );
454
454
enforce(json[" type" ].str == " struct" , " json .type " ~ json.toPrettyString ~ " is not a sruct" );
455
- enforce(json[" value" ].type == JSONType.object, " json .value " ~ json.toPrettyString ~ " is not an object" );
455
+ enforce(json[" value" ].type == JSONType.object || json[" value" ].type == JSONType.array,
456
+ " json .value " ~ json.toPrettyString ~ " must be an object or a list" );
456
457
if (auto structId = (" __struct_id" in json))
457
458
id = structId.get ! uint32_t ;
458
- foreach (ref label ; json[" value" ].objectKeyOrder){
459
- children[label] = GffValue(json[" value" ][label]);
459
+
460
+ if (json[" value" ].type == JSONType.array){
461
+ foreach (ref entry ; json[" value" ].array){
462
+ auto label = entry[" label" ].str;
463
+ children.dirtyAppendKeyValue(label, GffValue (entry));
464
+ }
465
+ }
466
+ else {
467
+ foreach (ref label ; json[" value" ].objectKeyOrder){
468
+ children[label] = GffValue(json[" value" ][label]);
469
+ }
460
470
}
461
471
}
462
472
// / GffStruct to JSON
463
- nwnlibd.orderedjson.JSONValue toJson () const {
473
+ nwnlibd.orderedjson.JSONValue toJson (bool structAsList = true ) const {
464
474
JSONValue ret;
465
475
ret[" type" ] = " struct" ;
466
476
ret[" __struct_id" ] = id;
467
- ret[" value" ] = JSONValue(cast (JSONValue[string ])null );
468
- foreach (ref kv ; children.byKeyValue ){
469
- ret[" value" ][kv.key] = kv.value.toJson();
477
+ if (structAsList){
478
+ ret[" value" ] = JSONValue(cast (JSONValue[])null );
479
+ foreach (ref kv ; children.byKeyValue ){
480
+ auto value = kv.value.toJson();
481
+ value[" label" ] = kv.key;
482
+
483
+ // Reorder label
484
+ const idx = value.objectKeyOrder[$ - 1 ];
485
+ value.objectKeyOrder = idx ~ value.objectKeyOrder[0 .. $ - 1 ];
486
+
487
+ ret[" value" ].array ~= value;
488
+ // ret["value"].array ~= JSONValue(["label": JSONValue(kv.key), "value": kv.value.toJson()]);
489
+ }
490
+ }
491
+ else {
492
+ ret[" value" ] = JSONValue(cast (JSONValue[string ])null );
493
+ foreach (ref kv ; children.byKeyValue ){
494
+ ret[" value" ][kv.key] = kv.value.toJson();
495
+ }
470
496
}
471
497
return ret;
472
498
}
@@ -529,13 +555,13 @@ struct GffList {
529
555
}
530
556
}
531
557
// / GffList to JSON
532
- nwnlibd.orderedjson.JSONValue toJson () const {
558
+ nwnlibd.orderedjson.JSONValue toJson (bool structAsList = true ) const {
533
559
JSONValue ret;
534
560
ret[" type" ] = " list" ;
535
561
ret[" value" ] = JSONValue(cast (JSONValue[])null );
536
562
ret[" value" ].array.length = children.length;
537
563
foreach (i, ref child ; children){
538
- ret[" value" ][i] = child.toJson();
564
+ ret[" value" ][i] = child.toJson(structAsList );
539
565
}
540
566
return ret;
541
567
}
@@ -701,7 +727,7 @@ struct GffValue {
701
727
}
702
728
}
703
729
// / Converts to JSON
704
- nwnlibd.orderedjson.JSONValue toJson () const {
730
+ nwnlibd.orderedjson.JSONValue toJson (bool structAsList = true ) const {
705
731
JSONValue ret;
706
732
final switch (type) with (GffType) {
707
733
case Byte: ret[" value" ] = get ! GffByte; break ;
@@ -718,8 +744,8 @@ struct GffValue {
718
744
case ResRef: ret[" value" ] = get ! GffResRef.value; break ;
719
745
case LocString: return get ! GffLocString.toJson;
720
746
case Void: ret[" value" ] = Base64.encode(get ! GffVoid).to! string ; break ;
721
- case Struct: return get ! GffStruct.toJson;
722
- case List: return get ! GffList.toJson;
747
+ case Struct: return get ! GffStruct.toJson(structAsList) ;
748
+ case List: return get ! GffList.toJson(structAsList) ;
723
749
case Invalid: assert (0 , " No type set" );
724
750
}
725
751
ret[" type" ] = type.gffTypeToCompatStr;
@@ -796,8 +822,8 @@ class Gff{
796
822
}
797
823
798
824
// / Convert to JSON
799
- nwnlibd.orderedjson.JSONValue toJson () const {
800
- auto ret = root.toJson();
825
+ nwnlibd.orderedjson.JSONValue toJson (bool structAsList = true ) const {
826
+ auto ret = root.toJson(structAsList );
801
827
ret[" __data_type" ] = fileType;
802
828
ret[" __data_version" ] = fileVersion;
803
829
return ret;
0 commit comments