IndexedRecordJsonObjectConverter extends RejectHandler { public JsonObject toJsonObject(IndexedRecord indexedRecord) throws IOException { JsonObjectBuilder objectBuilder = Json.createObjectBuilder(); Map<String, JsonObjectBuilder> nestedJsonObjectBuilders = new HashMap<>(); for (Schema.Field field : _schema.getFields()) { String fieldName = field.name(); Schema.Field dataField = SchemaUtils.getField( fieldName, indexedRecord.getSchema()); if ((dataField == null) || (indexedRecord.get(dataField.pos()) == null)) { if (!AvroUtils.isNullable(field.schema())) { reject( indexedRecord, new ConverterException( "Missing required field " + fieldName)); } continue; } Schema fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); JsonObjectBuilder currentJsonObjectBuilder = objectBuilder; if (_isNestedFieldName(fieldName)) { String[] nameParts = fieldName.split("_"); if (!nestedJsonObjectBuilders.containsKey(nameParts[0])) { nestedJsonObjectBuilders.put( nameParts[0], Json.createObjectBuilder()); } currentJsonObjectBuilder = nestedJsonObjectBuilders.get( nameParts[0]); fieldName = nameParts[1]; } int fieldPos = dataField.pos(); if (AvroUtils.isSameType(fieldSchema, AvroUtils._boolean())) { currentJsonObjectBuilder.add( fieldName, (boolean)indexedRecord.get(fieldPos)); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._bytes())) { Base64.Encoder encoder = Base64.getEncoder(); currentJsonObjectBuilder.add( fieldName, encoder.encodeToString( (byte[])indexedRecord.get(fieldPos))); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._decimal())) { currentJsonObjectBuilder.add( fieldName, (BigDecimal)indexedRecord.get(fieldPos)); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) { currentJsonObjectBuilder.add( fieldName, (double)indexedRecord.get(fieldPos)); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._float())) { currentJsonObjectBuilder.add( fieldName, (float)indexedRecord.get(fieldPos)); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._int())) { currentJsonObjectBuilder.add( fieldName, (int)indexedRecord.get(fieldPos)); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._long())) { if ((fieldSchema.getLogicalType() != null) && (fieldSchema.getLogicalType() == LogicalTypes.timestampMillis())) { currentJsonObjectBuilder.add( fieldName, _asISO8601String((long)indexedRecord.get(fieldPos))); continue; } currentJsonObjectBuilder.add( fieldName, (long)indexedRecord.get(fieldPos)); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._string())) { String stringFieldValue = (String)indexedRecord.get(fieldPos); if (Objects.equals("true", field.getProp("oas.dictionary")) || Objects.equals("Dictionary", fieldSchema.getName()) || _isJsonObjectFormattedString(stringFieldValue) || _isJsonArrayFormattedString(stringFieldValue)) { StringReader stringReader = new StringReader( stringFieldValue); JsonReader jsonReader = Json.createReader(stringReader); currentJsonObjectBuilder.add( fieldName, jsonReader.readValue()); jsonReader.close(); continue; } currentJsonObjectBuilder.add(fieldName, stringFieldValue); } else { reject( indexedRecord, new ConverterException( String.format( "Unable to convert field %s of type %s", fieldName, fieldSchema.getType()))); } } for (Map.Entry<String, JsonObjectBuilder> nestedJsonObjectBuilder : nestedJsonObjectBuilders.entrySet()) { objectBuilder.add( nestedJsonObjectBuilder.getKey(), nestedJsonObjectBuilder.getValue()); } return objectBuilder.build(); } IndexedRecordJsonObjectConverter(
		Boolean dieOnError, Schema schema, Schema rejectSchema, Result result); JsonObject toJsonObject(IndexedRecord indexedRecord);  }
@Test public void testToJsonObjectWithArrayProperties() throws Exception { JsonObjectIndexedRecordConverter jsonObjectIndexedRecordConverter = getJsonObjectIndexedRecordConverter( "/v1.0/catalogs/{siteId}/products", OASConstants.OPERATION_GET, readObject("openapi.json")); IndexedRecord indexedRecord = jsonObjectIndexedRecordConverter.toIndexedRecord( readObject("product_with_nested_images_content.json")); Object imagesArray = indexedRecord.get(18); Assert.assertEquals( "Images field type", String.class, imagesArray.getClass()); IndexedRecordJsonObjectConverter indexedRecordJsonObjectConverter = new IndexedRecordJsonObjectConverter( true, getSchema( "/v1.0/catalogs/{siteId}/product", OASConstants.OPERATION_POST, readObject("openapi.json")), null, new Result()); JsonObject jsonObject = indexedRecordJsonObjectConverter.toJsonObject( indexedRecord); JsonArray imagesJsonArray = jsonObject.getJsonArray("images"); Assert.assertNotNull(imagesJsonArray); JsonValue jsonValue = imagesJsonArray.get(0); Assert.assertTrue( "Array item is JSON object", jsonValue instanceof JsonObject); JsonObject itemJsonObject = jsonValue.asJsonObject(); Assert.assertEquals( "attachment value", "U29tZXRleHQsIHRoaXMgaXMgbm90IGltYWdl", itemJsonObject.getString("attachment")); JsonNumber idJsonNumber = itemJsonObject.getJsonNumber("id"); Assert.assertEquals("id value", 1977L, idJsonNumber.longValue()); JsonValue titleJsonValue = itemJsonObject.get("title"); Assert.assertTrue( "Title field type is JsonObject", titleJsonValue instanceof JsonObject); JsonObject titleJsonObject = titleJsonValue.asJsonObject(); Assert.assertEquals( "Title field value", "Image of me", titleJsonObject.getString("en_US")); }
@Test public void testToJsonObjectWithDictionaryProperties() throws Exception { JsonObjectIndexedRecordConverter jsonObjectIndexedRecordConverter = getJsonObjectIndexedRecordConverter( "/v1.0/catalogs/{siteId}/products", OASConstants.OPERATION_GET, readObject("openapi.json")); IndexedRecord indexedRecord = jsonObjectIndexedRecordConverter.toIndexedRecord( readObject("product_content.json")); IndexedRecordJsonObjectConverter indexedRecordJsonObjectConverter = new IndexedRecordJsonObjectConverter( true, getSchema( "/v1.0/catalogs/{siteId}/product", OASConstants.OPERATION_POST, readObject("openapi.json")), null, new Result()); JsonObject jsonObject = indexedRecordJsonObjectConverter.toJsonObject( indexedRecord); JsonObject descriptionJsonObject = jsonObject.getJsonObject( "description"); Assert.assertNotNull(descriptionJsonObject); Assert.assertEquals( "dictionary value", "Description of DXP Subscription", descriptionJsonObject.getString("en_US")); }
@Test public void testToJsonObjectWithTimeProperties() throws Exception { JsonObjectIndexedRecordConverter jsonObjectIndexedRecordConverter = getJsonObjectIndexedRecordConverter( "/v1.0/timestamp/{code}", OASConstants.OPERATION_GET, readObject("openapi_data_types.json")); IndexedRecord indexedRecord = jsonObjectIndexedRecordConverter.toIndexedRecord( readObject("timestamp_content.json")); IndexedRecordJsonObjectConverter indexedRecordJsonObjectConverter = new IndexedRecordJsonObjectConverter( true, getSchema( "/v1.0/timestamp/{code}", OASConstants.OPERATION_PATCH, readObject("openapi_data_types.json")), null, new Result()); JsonObject jsonObject = indexedRecordJsonObjectConverter.toJsonObject( indexedRecord); Assert.assertEquals( "timestamp1 value", "2019-12-02T05:17:09Z", jsonObject.getString("timestamp1")); Assert.assertEquals( "timestamp2 value", "2019-12-02T05:17:09Z", jsonObject.getString("timestamp2")); Assert.assertEquals( "timestamp3 value", "2019-12-02T04:17:09Z", jsonObject.getString("timestamp3")); }