|
| 1 | +package se.liu.ida.hefquin.base.utils; |
| 2 | + |
| 3 | +import java.io.PrintStream; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.apache.jena.atlas.json.JsonObject; |
| 7 | +import org.apache.jena.atlas.json.JsonString; |
| 8 | +import org.apache.jena.atlas.json.JsonValue; |
| 9 | +import org.apache.commons.lang3.ArrayUtils; |
| 10 | +import org.apache.jena.atlas.json.JsonArray; |
| 11 | +import org.apache.jena.atlas.json.JsonBoolean; |
| 12 | +import org.apache.jena.atlas.json.JsonNumber; |
| 13 | +import org.apache.jena.atlas.json.JsonNull; |
| 14 | + |
| 15 | +/** |
| 16 | + * A utility class for converting and printing {@link Stats} objects in JSON format. |
| 17 | + */ |
| 18 | +public class StatsPrinterJSON |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Converts and prints the given {@link Stats} object as JSON to the specified {@link PrintStream}. |
| 22 | + * |
| 23 | + * @param s the {@link Stats} object to print |
| 24 | + * @param out the output stream to print to |
| 25 | + * @param recursive whether nested {@link Stats} entries should be printed recursively |
| 26 | + */ |
| 27 | + public static void print( final Stats s, final PrintStream out, final boolean recursive ) { |
| 28 | + final JsonObject stats = statsAsJson( s, recursive ); |
| 29 | + out.print( stats.toString() ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Converts the given {@link Stats} object as JSON. Nested {@link Stats} entries will be converted recursively. |
| 34 | + * |
| 35 | + * @param s the {@link Stats} object to convert |
| 36 | + */ |
| 37 | + public static JsonObject statsAsJson( final Stats s ) { |
| 38 | + return statsAsJson( s, true ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Converts the given {@link Stats} object to a JSON object. |
| 43 | + * |
| 44 | + * @param s the {@link Stats} object to convert |
| 45 | + * @param recursive whether nested {@link Stats} entries should be converted recursively |
| 46 | + * @return a JSON object representation of the {@link Stats} |
| 47 | + */ |
| 48 | + public static JsonObject statsAsJson( final Stats s, final boolean recursive ) { |
| 49 | + final JsonObject stats = new JsonObject(); |
| 50 | + if ( s == null || s.isEmpty() ) { |
| 51 | + return stats; |
| 52 | + } |
| 53 | + for ( final String entryName : s.getEntryNames() ) { |
| 54 | + final Object entry = s.getEntry( entryName ); |
| 55 | + stats.put( entryName, asJson( entry, recursive ) ); |
| 56 | + } |
| 57 | + |
| 58 | + return stats; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Converts the given {@link Stats} object to a JSON object. |
| 63 | + * |
| 64 | + * @param s the {@link Stats} object to convert |
| 65 | + * @param recursive whether nested {@link Stats} entries should be converted recursively |
| 66 | + * @return a JSON object representation of the {@link Stats} |
| 67 | + */ |
| 68 | + private static JsonValue asJson( final Object entry, final boolean recursive ) { |
| 69 | + if ( entry == null ) { |
| 70 | + return JsonNull.instance; |
| 71 | + } |
| 72 | + |
| 73 | + if ( entry instanceof Stats stats ) { |
| 74 | + if ( recursive ) { |
| 75 | + return statsAsJson( stats, recursive ); |
| 76 | + } |
| 77 | + return new JsonString( "..." ); |
| 78 | + } |
| 79 | + |
| 80 | + // List of objects |
| 81 | + if ( entry instanceof List<?> list ) { |
| 82 | + final JsonArray jsonArray = new JsonArray(); |
| 83 | + for ( final Object o : list ) { |
| 84 | + jsonArray.add( asJson( o, recursive ) ); |
| 85 | + } |
| 86 | + return jsonArray; |
| 87 | + } |
| 88 | + |
| 89 | + // Array of objects |
| 90 | + if ( entry instanceof Object[] array ) { |
| 91 | + final JsonArray arr = new JsonArray(); |
| 92 | + for ( Object o : array ) { |
| 93 | + arr.add( asJson( o, recursive ) ); |
| 94 | + } |
| 95 | + return arr; |
| 96 | + } |
| 97 | + |
| 98 | + // Array of primitives |
| 99 | + if ( entry instanceof int[] a ) { |
| 100 | + return asJson( ArrayUtils.toObject( a ), recursive ); |
| 101 | + } |
| 102 | + if ( entry instanceof long[] a ) { |
| 103 | + return asJson( ArrayUtils.toObject( a ), recursive ); |
| 104 | + } |
| 105 | + if ( entry instanceof double[] a ) { |
| 106 | + return asJson( ArrayUtils.toObject( a ), recursive ); |
| 107 | + } |
| 108 | + if ( entry instanceof float[] a ) { |
| 109 | + return asJson( ArrayUtils.toObject( a ), recursive ); |
| 110 | + } |
| 111 | + if ( entry instanceof boolean[] a ) { |
| 112 | + return asJson( ArrayUtils.toObject( a ), recursive ); |
| 113 | + } |
| 114 | + |
| 115 | + // JSON primitives |
| 116 | + if ( entry instanceof Boolean b ) { |
| 117 | + return new JsonBoolean( b ); |
| 118 | + } |
| 119 | + |
| 120 | + if ( entry instanceof Number num ) { |
| 121 | + if ( num instanceof Double d && (Double.isNaN( d ) || Double.isInfinite( d )) ) { |
| 122 | + return new JsonString( d.toString() ); |
| 123 | + } |
| 124 | + if ( num instanceof Float f && (Float.isNaN( f ) || Float.isInfinite( f )) ) { |
| 125 | + return new JsonString( f.toString() ); |
| 126 | + } |
| 127 | + return JsonNumber.value( num.doubleValue() ); |
| 128 | + } |
| 129 | + |
| 130 | + // default to string |
| 131 | + return new JsonString( entry.toString() ); |
| 132 | + } |
| 133 | +} |
0 commit comments