private void testObjectGraph (TestMapGraph object, String typeName) { Json json = new Json(); json.setTypeName(typeName); json.setUsePrototypes(false); json.setIgnoreUnknownFields(true); json.setOutputType(OutputType.json); String text = json.prettyPrint(object); TestMapGraph object2 = json.fromJson(TestMapGraph.class, text); if (object2.map.size() != object.map.size()) { throw new RuntimeException("Too many items in deserialized json map."); } if (object2.objectMap.size != object.objectMap.size) { throw new RuntimeException("Too many items in deserialized json object map."); } if (object2.arrayMap.size != object.arrayMap.size) { throw new RuntimeException("Too many items in deserialized json map."); } }
/** * Writes a JSON document by searching the object for * {@link org.mini2Dx.core.serialization.annotation.Field} annotations * * @param object * The object to convert to JSON * @param prettyPrint * Set to true if the JSON should be prettified * @return The object serialized as JSON * @throws SerializationException * Thrown when the object is invalid */ public <T> String toJson(T object, boolean prettyPrint) throws SerializationException { StringWriter writer = new StringWriter(); Json json = new Json(); json.setOutputType(OutputType.json); json.setWriter(writer); writeObject(null, object, null, json); String result = writer.toString(); try { writer.close(); } catch (IOException e) { throw new SerializationException(e); } if (prettyPrint) { return json.prettyPrint(result); } return result; }
public void saveModel(String chapterId) throws IOException { EngineLogger.debug("SAVING GAME MODEL"); if (w.isDisposed()) return; Json json = new BladeJson(w, Mode.MODEL); json.setOutputType(OutputType.javascript); String s = null; if (EngineLogger.debugMode()) s = json.prettyPrint(this); else s = json.toJson(this); Writer w = EngineAssetManager.getInstance().getModelFile(chapterId + EngineAssetManager.CHAPTER_EXT) .writer(false, "UTF-8"); try { w.write(s); w.flush(); } catch (IOException e) { throw new IOException("ERROR SAVING MODEL", e); } finally { w.close(); } }
json.setOutputType(JsonWriter.OutputType.json);
public void saveGameState(String filename, boolean screenshot) throws IOException { EngineLogger.debug("SAVING GAME STATE"); if (w.isDisposed()) return; Json json = new BladeJson(w, Mode.STATE); json.setOutputType(OutputType.javascript); String s = null; if (EngineLogger.debugMode()) s = json.prettyPrint(this); else s = json.toJson(this); Writer writer = EngineAssetManager.getInstance().getUserFile(filename).writer(false, "UTF-8"); try { writer.write(s); writer.flush(); } catch (IOException e) { throw new IOException("ERROR SAVING GAME", e); } finally { writer.close(); } // Save Screenshot if (screenshot) w.takeScreenshot(filename + ".png", SCREENSHOT_DEFAULT_WIDTH); }
public void saveWorldDesc(FileHandle file) throws IOException { float scale = EngineAssetManager.getInstance().getScale(); Json json = new BladeJson(w, Mode.MODEL); json.setOutputType(OutputType.javascript); json.setWriter(new StringWriter()); json.writeObjectStart(); json.writeValue("width", w.getWidth() / scale); json.writeValue("height", w.getHeight() / scale); json.writeValue("initChapter", w.getInitChapter()); w.getVerbManager().write(json); json.writeObjectEnd(); String s = null; if (EngineLogger.debugMode()) s = json.prettyPrint(json.getWriter().getWriter().toString()); else s = json.getWriter().getWriter().toString(); Writer w = file.writer(false, "UTF-8"); w.write(s); w.close(); }
json.setOutputType(JsonWriter.OutputType.json);