static public AnnotationTile getTileFromJSON( JSONObject json ) throws IllegalArgumentException { try { TileIndex index = new TileIndex( json.getInt("level"), json.getInt("x"), json.getInt("y"), AnnotationIndexer.NUM_BINS, AnnotationIndexer.NUM_BINS ); // create tile with empty bins DenseTileData<AnnotationBin> tile = new DenseTileData<>( index ); // for all binkeys Iterator<?> binKeys = json.keys(); while( binKeys.hasNext() ) { String binKey = (String)binKeys.next(); if( json.get(binKey) instanceof JSONObject ){ JSONObject bin = (JSONObject)json.get(binKey); BinIndex binIndex = BinIndex.fromString( binKey ); tile.setBin( binIndex.getX(), binIndex.getY(), getBinFromJSON( bin )); } } return new AnnotationTile( tile.getDefinition(), tile.getData() ); } catch ( Exception e ) { throw new IllegalArgumentException( e ); } }
@Test public void testReadWordScoreTile () throws IOException, JSONException { // Create a tile to test TileSerializer<List<Pair<String, Double>>> serializer = new PairArrayAvroSerializer<>(String.class, Double.class, CodecFactory.nullCodec()); TileIndex index = new TileIndex(0, 0, 0, 1, 1); DenseTileData<List<Pair<String, Double>>> tile = new DenseTileData<>(index); List<Pair<String, Double>> bin = new ArrayList<>(); bin.add(new Pair<String, Double>("abc", 1.0)); bin.add(new Pair<String, Double>("def", 1.5)); bin.add(new Pair<String, Double>("ghi", 2.0)); bin.add(new Pair<String, Double>("jkl", 2.25)); tile.setBin(0, 0, bin); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(tile, baos); baos.flush(); baos.close(); byte[] serializedTileData = baos.toByteArray(); // Now try to convert that to JSON. JSONObject result = AvroJSONConverter.convert(new ByteArrayInputStream(serializedTileData)); System.out.println(result.toString()); } }
public TileData<List<T>> harden () { TileIndex index = getDefinition(); if (isDense()) { DenseTileData<List<T>> hardened = new DenseTileData<>(index); hardened.setDefaultValue(_base.getDefaultValue()); for (int x=0; x<index.getXBins(); ++x) { for (int y=0; y<index.getYBins(); ++y) { hardened.setBin(x, y, getBin(x, y)); } } return hardened; } else { SparseTileData<List<T>> hardened = new SparseTileData<>(index, _base.getDefaultValue()); for (int x=0; x<index.getXBins(); ++x) { for (int y=0; y<index.getYBins(); ++y) { List<T> value = getBin(x, y); if (null != value) { hardened.setBin(x, y, getBin(x, y)); } } } return hardened; } }