private String writeJson ( Object data , Map < String , Object > meta ) throws IOException , NoSuchFieldException , IllegalAccessException , ODataEdmException , ODataRenderException { ByteArrayOutputStream stream = new ByteArrayOutputStream ( ) ; jsonGenerator = JSON_FACTORY . createGenerator ( stream , JsonEncoding . UTF8 ) ; jsonGenerator . writeStartObject ( ) ; // Write @odata constants entitySet = ( data instanceof List ) ? getEntitySet ( ( List < ? > ) data ) : getEntitySet ( data ) ; jsonGenerator . writeStringField ( CONTEXT , contextURL ) ; // Write @odata.count if requested and provided. if ( hasCountOption ( odataUri ) && data instanceof List && meta != null && meta . containsKey ( "count" ) ) { long count ; Object countObj = meta . get ( "count" ) ; if ( countObj instanceof Integer ) { count = ( ( Integer ) countObj ) . longValue ( ) ; } else { count = ( long ) countObj ; } jsonGenerator . writeNumberField ( COUNT , count ) ; } if ( ! ( data instanceof List ) ) { if ( entitySet != null ) { jsonGenerator . writeStringField ( ID , String . format ( "%s(%s)" , getEntityName ( entityDataModel , data ) , formatEntityKey ( entityDataModel , data ) ) ) ; } else { jsonGenerator . writeStringField ( ID , String . format ( "%s" , getEntityName ( entityDataModel , data ) ) ) ; } } // Write feed if ( data instanceof List ) { marshallEntities ( ( List < ? > ) data ) ; } else { marshall ( data , this . entityDataModel . getType ( data . getClass ( ) ) ) ; } jsonGenerator . writeEndObject ( ) ; jsonGenerator . close ( ) ; return stream . toString ( StandardCharsets . UTF_8 . name ( ) ) ; }
Write the given data to the JSON stream . The data to write will be either a single entity or a feed depending on whether it is a single object or list .