@@ -422,6 +422,58 @@ public void testJsonStringWithEmptyArray() throws IOException {
422422 }
423423 }
424424
425+ @ Test
426+ public void testJsonStringWithStringArray () throws IOException {
427+ Schema recordSchema = Schema .recordOf ("record" ,
428+ Schema .Field .of ("arrayOfString" , Schema .arrayOf (Schema .of (Schema .Type .STRING ))));
429+ List <String > jsonStringList = ImmutableList .of ("{\" arrayKey1\" : \" arrayValue1\" }" ,
430+ "{\" arrayKey2\" : \" arrayValue2\" }" );
431+ StructuredRecord record = StructuredRecord .builder (recordSchema ).set ("arrayOfString" , jsonStringList ).build ();
432+ Set <String > jsonStringFieldsPaths = ImmutableSet .of ("arrayOfString" );
433+ try (JsonTreeWriter writer = new JsonTreeWriter ()) {
434+ writer .beginObject ();
435+ for (Schema .Field recordField : Objects .requireNonNull (record .getSchema ().getFields ())) {
436+ if (recordSchema .getField (recordField .getName ()) != null ) {
437+ BigQueryRecordToJson .write (writer , recordField .getName (), record .get (recordField .getName ()),
438+ recordField .getSchema (), jsonStringFieldsPaths );
439+ }
440+ }
441+ writer .endObject ();
442+ JsonObject actual = writer .get ().getAsJsonObject ();
443+ String actualJsonString = actual .get ("arrayOfString" ).getAsJsonArray ().toString ();
444+ String expectedJsonString = "[{\" arrayKey1\" :\" arrayValue1\" },{\" arrayKey2\" :\" arrayValue2\" }]" ;
445+ Assert .assertEquals (expectedJsonString , actualJsonString );
446+ }
447+ }
448+
449+ @ Test
450+ public void testJsonStringWithArrayAndNestedRecord () throws IOException {
451+ Schema nestedRecordSchema = Schema .recordOf ("nestedRecord" ,
452+ Schema .Field .of ("nestedJsonString" , Schema .of (Schema .Type .STRING )));
453+ StructuredRecord nestedRecord = StructuredRecord .builder (nestedRecordSchema )
454+ .set ("nestedJsonString" , "{\" nestedKey1\" :\" nestedValue1\" }" ).build ();
455+ Schema recordSchema = Schema .recordOf ("record" ,
456+ Schema .Field .of ("arrayOfNestedRecord" , Schema .arrayOf (nestedRecordSchema )));
457+ List <StructuredRecord > nestedRecordList = ImmutableList .of (nestedRecord );
458+ StructuredRecord record = StructuredRecord .builder (recordSchema ).set ("arrayOfNestedRecord" , nestedRecordList )
459+ .build ();
460+
461+ Set <String > jsonStringFieldsPaths = ImmutableSet .of ("arrayOfNestedRecord.nestedJsonString" );
462+ try (JsonTreeWriter writer = new JsonTreeWriter ()) {
463+ writer .beginObject ();
464+ for (Schema .Field recordField : Objects .requireNonNull (record .getSchema ().getFields ())) {
465+ if (recordSchema .getField (recordField .getName ()) != null ) {
466+ BigQueryRecordToJson .write (writer , recordField .getName (), record .get (recordField .getName ()),
467+ recordField .getSchema (), jsonStringFieldsPaths );
468+ }
469+ }
470+ writer .endObject ();
471+ JsonObject actual = writer .get ().getAsJsonObject ();
472+ String actualJsonString = actual .get ("arrayOfNestedRecord" ).toString ();
473+ String expectedJsonString = "[{\" nestedJsonString\" :{\" nestedKey1\" :\" nestedValue1\" }}]" ;
474+ Assert .assertEquals (expectedJsonString , actualJsonString );
475+ }
476+ }
425477
426478 /**
427479 * Empty JSON string is not a valid JSON string and should throw an exception.
0 commit comments