@@ -171,4 +171,99 @@ public static void appendAsString(
171171 break ;
172172 }
173173 }
174+
175+ /**
176+ * Append an encoding as a Json String to a {@link StringBuilder}.
177+ *
178+ * @param sb to append the encoding to.
179+ * @param buffer containing the encoded value.
180+ * @param index at which the encoded value exists.
181+ * @param encoding representing the encoded value.
182+ */
183+ public static void appendAsJsonString (
184+ final StringBuilder sb , final DirectBuffer buffer , final int index , final Encoding encoding )
185+ {
186+ switch (encoding .primitiveType ())
187+ {
188+ case CHAR :
189+ sb .append ('\'' ).append ((char )buffer .getByte (index )).append ('\'' );
190+ break ;
191+
192+ case INT8 :
193+ sb .append (buffer .getByte (index ));
194+ break ;
195+
196+ case INT16 :
197+ sb .append (buffer .getShort (index , encoding .byteOrder ()));
198+ break ;
199+
200+ case INT32 :
201+ sb .append (buffer .getInt (index , encoding .byteOrder ()));
202+ break ;
203+
204+ case INT64 :
205+ sb .append (buffer .getLong (index , encoding .byteOrder ()));
206+ break ;
207+
208+ case UINT8 :
209+ sb .append ((short )(buffer .getByte (index ) & 0xFF ));
210+ break ;
211+
212+ case UINT16 :
213+ sb .append (buffer .getShort (index , encoding .byteOrder ()) & 0xFFFF );
214+ break ;
215+
216+ case UINT32 :
217+ sb .append (buffer .getInt (index , encoding .byteOrder ()) & 0xFFFF_FFFFL );
218+ break ;
219+
220+ case UINT64 :
221+ sb .append (buffer .getLong (index , encoding .byteOrder ()));
222+ break ;
223+
224+ case FLOAT :
225+ {
226+ final float value = buffer .getFloat (index , encoding .byteOrder ());
227+ if (Float .isNaN (value ))
228+ {
229+ sb .append ("0/0" );
230+ }
231+ else if (value == Float .POSITIVE_INFINITY )
232+ {
233+ sb .append ("1/0" );
234+ }
235+ else if (value == Float .NEGATIVE_INFINITY )
236+ {
237+ sb .append ("-1/0" );
238+ }
239+ else
240+ {
241+ sb .append (value );
242+ }
243+ break ;
244+ }
245+
246+ case DOUBLE :
247+ {
248+ final double value = buffer .getDouble (index , encoding .byteOrder ());
249+ if (Double .isNaN (value ))
250+ {
251+ sb .append ("0/0" );
252+ }
253+ else if (value == Double .POSITIVE_INFINITY )
254+ {
255+ sb .append ("1/0" );
256+ }
257+ else if (value == Double .NEGATIVE_INFINITY )
258+ {
259+ sb .append ("-1/0" );
260+ }
261+ else
262+ {
263+ sb .append (value );
264+ }
265+ break ;
266+ }
267+ }
268+ }
174269}
0 commit comments