com.badlogic.gdx.utils.Json类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(167)

本文整理了Java中com.badlogic.gdx.utils.Json类的一些代码示例,展示了Json类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json类的具体详情如下:
包路径:com.badlogic.gdx.utils.Json
类名称:Json

Json介绍

[英]Reads/writes Java objects to/from JSON, automatically. See the wiki for usage: https://github.com/libgdx/libgdx/wiki/Reading-%26-writing-JSON
[中]自动在JSON中读取/写入Java对象。有关用法,请参见wiki:https://github.com/libgdx/libgdx/wiki/Reading-%26-编写JSON

代码示例

代码示例来源:origin: libgdx/libgdx

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.");
  }
}

代码示例来源:origin: libgdx/libgdx

public String prettyPrint (Object object, int singleLineColumns) {
  return prettyPrint(toJson(object), singleLineColumns);
}

代码示例来源:origin: libgdx/libgdx

@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);
}

代码示例来源:origin: libgdx/libgdx

/** @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;
  }
}

代码示例来源:origin: libgdx/libgdx

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;

代码示例来源:origin: libgdx/libgdx

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]");

代码示例来源:origin: libgdx/libgdx

@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;
}

代码示例来源:origin: libgdx/libgdx

|| 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;

代码示例来源:origin: libgdx/libgdx

@Override
  public void read (Json json, JsonValue jsonData) {
    regions.clear();
    regions.addAll(json.readValue("regions", Array.class, AspectTextureRegion.class, jsonData));
  }
}

代码示例来源:origin: kotcrab/vis-ui

public Array<FileHandle> loadRecentDirectories () {
  String data = prefs.getString(recentDirKeyName, null);
  if (data == null)
    return new Array<FileHandle>();
  else
    return json.fromJson(FileArrayData.class, data).toFileHandleArray();
}

代码示例来源:origin: libgdx/libgdx

@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);
}

代码示例来源:origin: SquidPony/SquidLib

@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();
}

代码示例来源:origin: libgdx/libgdx

@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);
}

代码示例来源:origin: crashinvaders/gdx-texture-packer-gui

@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();
}

代码示例来源:origin: libgdx/libgdx

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);

代码示例来源:origin: libgdx/libgdx

/** 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;
  }
}

代码示例来源:origin: SquidPony/SquidLib

@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();
}

代码示例来源:origin: junkdog/artemis-odb

@Override
public void write(Json json, Bag bag, Class knownType) {
  json.writeArrayStart();
  for (Object item : bag)
    json.writeValue(item);
  json.writeArrayEnd();
}

代码示例来源:origin: libgdx/libgdx

println("Copied field by field: " + fieldCopy);
Json json = new Json();
String jsonString = json.toJson(fromCopyConstructor);
Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
println("JSON serialized: " + jsonString);
println("JSON deserialized: " + fromJson);

代码示例来源:origin: libgdx/libgdx

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);
  }
}

相关文章