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."); } }
public String prettyPrint (Object object, int singleLineColumns) { return prettyPrint(toJson(object), singleLineColumns); }
@Override public void write (Json json) { json.writeValue("unique", uniqueData, ObjectMap.class); json.writeValue("data", data, Array.class, SaveData.class); json.writeValue("assets", sharedAssets.toArray(AssetData.class), AssetData[].class); json.writeValue("resource", resource, null); }
/** @param knownType May be null if the type is unknown. * @param elementType May be null if the type is unknown. */ public void toJson (Object object, Class knownType, Class elementType, Writer writer) { setWriter(writer); try { writeValue(object, knownType, elementType); } finally { StreamUtils.closeQuietly(this.writer); this.writer = null; } }
String className = typeName == null ? null : jsonData.getString(typeName, null); if (className != null) { type = getClass(className); if (type == null) { try { || type == Long.class || type == Double.class || type == Short.class || type == Byte.class || type == Character.class || ClassReflection.isAssignableFrom(Enum.class, type)) { return readValue("value", type, jsonData); Object object = newInstance(type); ObjectMap result = (ObjectMap)object; for (JsonValue child = jsonData.child; child != null; child = child.next) result.put(child.name, readValue(elementType, null, child)); ArrayMap result = (ArrayMap)object; for (JsonValue child = jsonData.child; child != null; child = child.next) result.put(child.name, readValue(elementType, null, child)); result.put(child.name, readValue(elementType, null, child)); readFields(object, jsonData); return (T)object; Object object = newInstance(type); ((Serializable)object).read(this, jsonData); return (T)object;
public void create () { json = new Json(); test.stringField = "stringvalue"; test.byteArrayField = new byte[] {2, 1, 0, -1, -2}; test.map = new ObjectMap(); test.map.put("one", 1); test.map.put("two", 2); test.map.put("nine", 9); test.stringArray = new Array(); test.stringArray.add("meow"); test.stringArray.add("moo"); test.objectArray = new Array(); test.objectArray.add("meow"); roundTrip(test); equals(json.toJson("meow"), "meow"); equals(json.toJson("meow "), "\"meow \""); equals(json.toJson(" meow"), "\" meow\""); equals(json.toJson(" meow "), "\" meow \""); equals(json.toJson("\nmeow\n"), "\\nmeow\\n"); equals(json.toJson(Array.with(1, 2, 3), null, int.class), "[1,2,3]"); equals(json.toJson(Array.with("1", "2", "3"), null, String.class), "[1,2,3]"); equals(json.toJson(Array.with(" 1", "2 ", " 3 "), null, String.class), "[\" 1\",\"2 \",\" 3 \"]"); equals(json.toJson(Array.with("1", "", "3"), null, String.class), "[1,\"\",3]");
@Override public Array<AssetDescriptor> getDependencies (String fileName, FileHandle file, ParticleEffectLoadParameter parameter) { Json json = new Json(); ResourceData<ParticleEffect> data = json.fromJson(ResourceData.class, file); Array<AssetData> assets = null; synchronized (items) { ObjectMap.Entry<String, ResourceData<ParticleEffect>> entry = new ObjectMap.Entry<String, ResourceData<ParticleEffect>>(); entry.key = fileName; entry.value = data; items.add(entry); assets = data.getAssets(); } Array<AssetDescriptor> descriptors = new Array<AssetDescriptor>(); for (AssetData<?> assetData : assets) { // If the asset doesn't exist try to load it from loading effect directory if (!resolve(assetData.filename).exists()) { assetData.filename = file.parent().child(Gdx.files.internal(assetData.filename).name()).path(); } if (assetData.type == ParticleEffect.class) { descriptors.add(new AssetDescriptor(assetData.filename, assetData.type, parameter)); } else descriptors.add(new AssetDescriptor(assetData.filename, assetData.type)); } return descriptors; }
|| actualType == Float.class || actualType == Long.class || actualType == Double.class || actualType == Short.class || actualType == Byte.class || actualType == Character.class) { writeObjectStart(actualType, null); writeValue("value", value); writeObjectEnd(); return; writeObjectStart(actualType, knownType); ((Serializable)value).write(this); writeObjectEnd(); return; throw new SerializationException("Serialization of an Array other than the known type is not supported.\n" + "Known type: " + knownType + "\nActual type: " + actualType); writeArrayStart(); Array array = (Array)value; for (int i = 0, n = array.size; i < n; i++) writeValue(array.get(i), elementType, null); writeArrayEnd(); return; throw new SerializationException("Serialization of a Queue other than the known type is not supported.\n" + "Known type: " + knownType + "\nActual type: " + actualType); writeArrayStart(); Queue queue = (Queue)value; for (int i = 0, n = queue.size; i < n; i++) writeValue(queue.get(i), elementType, null); writeArrayEnd(); return;
@Override public void read (Json json, JsonValue jsonMap) { name = json.readValue("name", String.class, jsonMap); emitter = json.readValue("emitter", Emitter.class, jsonMap); influencers.addAll(json.readValue("influencers", Array.class, Influencer.class, jsonMap)); renderer = json.readValue("renderer", ParticleControllerRenderer.class, jsonMap); }
@Override public void write(Json json, Arrangement object, Class knownType) { if(object == null) { json.writeValue(null); return; } json.writeObjectStart(); json.writeValue("k", object.keysAsOrderedSet(), OrderedSet.class); json.writeValue("f", object.f); json.writeObjectEnd(); }
@Override public void read (Json json, JsonValue jsonData) { uniqueData = json.readValue("unique", ObjectMap.class, jsonData); for (Entry<String, SaveData> entry : uniqueData.entries()) { entry.value.resources = this; } data = json.readValue("data", Array.class, SaveData.class, jsonData); for (SaveData saveData : data) { saveData.resources = this; } sharedAssets.addAll(json.readValue("assets", Array.class, AssetData.class, jsonData)); resource = json.readValue("resource", null, jsonData); }
@Initiate(priority = AutumnActionPriority.TOP_PRIORITY) void init() { modulesDir.mkdirs(); if (repoCacheFile.exists()) { Array<RepositoryModuleData> newArray = json.fromJson(Array.class, RepositoryModuleData.class, repoCacheFile); repositoryModules.clear(); for (RepositoryModuleData moduleData : newArray) { repositoryModules.put(moduleData.name, moduleData); } Gdx.app.log(TAG, "Cached data was loaded"); } requestRefreshRepositoryIfNeeded(); }
public void readFields (Object object, JsonValue jsonMap) { Class type = object.getClass(); ObjectMap<String, FieldMetadata> fields = getFields(type); for (JsonValue child = jsonMap.child; child != null; child = child.next) { FieldMetadata metadata = fields.get(child.name().replace(" ", "_")); if (metadata == null) { if (child.name.equals(typeName)) continue; if (ignoreUnknownFields || ignoreUnknownField(type, child.name)) { if (debug) System.out.println("Ignoring unknown field: " + child.name + " (" + type.getName() + ")"); continue; field.set(object, readValue(field.getType(), metadata.elementType, child)); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
/** Writes the specified field to the current JSON object. * @param elementType May be null if the type is unknown. */ public void writeField (Object object, String fieldName, String jsonName, Class elementType) { Class type = object.getClass(); ObjectMap<String, FieldMetadata> fields = getFields(type); FieldMetadata metadata = fields.get(fieldName); if (metadata == null) throw new SerializationException("Field not found: " + fieldName + " (" + type.getName() + ")"); Field field = metadata.field; if (elementType == null) elementType = metadata.elementType; try { if (debug) System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")"); writer.name(jsonName); writeValue(field.get(object), field.getType(), elementType); } catch (ReflectionException ex) { throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); } catch (SerializationException ex) { ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } catch (Exception runtimeEx) { SerializationException ex = new SerializationException(runtimeEx); ex.addTrace(field + " (" + type.getName() + ")"); throw ex; } }
@Override public void write(Json json, IntDoubleOrderedMap object, Class knownType) { if(object == null) { json.writeValue(null); return; } json.writeObjectStart(); json.writeArrayStart("k"); IntDoubleOrderedMap.KeyIterator ki = object.keySet().iterator(); while (ki.hasNext()) json.writeValue(ki.nextInt()); json.writeArrayEnd(); json.writeArrayStart("v"); IntDoubleOrderedMap.DoubleIterator vi = object.values().iterator(); while (vi.hasNext()) json.writeValue(vi.nextDouble()); json.writeArrayEnd(); json.writeValue("f", object.f); json.writeObjectEnd(); }
@Override public void write(Json json, Bag bag, Class knownType) { json.writeArrayStart(); for (Object item : bag) json.writeValue(item); json.writeArrayEnd(); }
static public void main (String[] args) throws Exception { Settings settings = null; String input = null, output = null, packFileName = "pack.atlas"; switch (args.length) { case 4: settings = new Json().fromJson(Settings.class, new FileReader(args[3])); case 3: packFileName = args[2]; case 2: output = args[1]; case 1: input = args[0]; break; default: System.out.println("Usage: inputDir [outputDir] [packFileName] [settingsFileName]"); System.exit(0); } if (output == null) { File inputFile = new File(input); output = new File(inputFile.getParentFile(), inputFile.getName() + "-packed").getAbsolutePath(); } if (settings == null) settings = new Settings(); process(settings, input, output, packFileName); } }