Example #1
0
 public String constructJsonString() {
   String str = "";
   Json json = new Json();
   json.setOutputType(OutputType.json);
   str = json.toJson(this);
   json.prettyPrint(str);
   return str;
 }
Example #2
0
  public static void writeExample() {
    Levels levels = new Levels();
    levels.levels = new ArrayList<LevelDefinition>();

    {
      LevelDefinition ld = new LevelDefinition();
      ld.start_time = 7;
      ld.groups = new ArrayList<ProbabilityGroup>();

      ProbabilityGroup pg1 = new ProbabilityGroup();
      pg1.chance = 0.3f;
      pg1.sequences = new ArrayList<String>();
      pg1.sequences.add("a");
      pg1.sequences.add("b");

      ProbabilityGroup pg2 = new ProbabilityGroup();
      pg2.chance = 0.7f;
      pg2.sequences = new ArrayList<String>();
      pg2.sequences.add("c");
      pg2.sequences.add("d");
      pg2.sequences.add("e");

      ld.groups.add(pg1);
      ld.groups.add(pg2);

      levels.levels.add(ld);
    }
    {
      LevelDefinition ld = new LevelDefinition();
      ld.start_time = 21;
      ld.groups = new ArrayList<ProbabilityGroup>();

      ProbabilityGroup pg1 = new ProbabilityGroup();
      pg1.chance = 0.4f;
      pg1.sequences = new ArrayList<String>();
      pg1.sequences.add("f");
      pg1.sequences.add("g");
      pg1.sequences.add("h");

      ProbabilityGroup pg2 = new ProbabilityGroup();
      pg2.chance = 0.6f;
      pg2.sequences = new ArrayList<String>();
      pg2.sequences.add("i");
      pg2.sequences.add("j");

      ld.groups.add(pg1);
      ld.groups.add(pg2);

      levels.levels.add(ld);
    }

    String s = json.prettyPrint(levels);
    Gdx.files.local(PATH).writeString(s, false);
  }
  public static void Save(SaveData data) {

    saveData = data;

    FileHandle file = Gdx.files.local("saves/savedata.json");

    if (file.exists()) {
      file.delete();
    }

    String savestring = json.prettyPrint(saveData);
    file.writeString(savestring, true);
  }
  public static <T> void write(
      FileHandle path, T object, Class<T> clazz, Consumer<Json> setupJson) {

    Json json = new Json(JsonWriter.OutputType.json);
    json.setSerializer(clazz, new AnnotatedJsonSerializer<>(json, clazz));

    if (setupJson != null) {
      setupJson.accept(json);
    }

    String output = json.toJson(object);
    String prettyOutput = json.prettyPrint(output);

    path.writeString(prettyOutput, false, "UTF-8");
  }
  public static void ResetSave() {
    saveData =
        new SaveData(
            new PlayerData(PlayerData.AbilityType.NONE, 0, 0, 0, 0, PlayerData.SkinType.HUMAN),
            new LevelSaveData[] {new LevelSaveData(0, 0, true), new LevelSaveData(0, 0, false)},
            0);

    FileHandle file = Gdx.files.local("saves/savedata.json");

    if (file.exists()) {
      file.delete();
    }

    String savestring = json.prettyPrint(saveData);
    file.writeString(savestring, true);
  }
  public static void Initialize() {

    FileHandle file = Gdx.files.local("saves/savedata.json");
    if (!file.exists()) {

      String savestring =
          json.prettyPrint(
              new SaveData(
                  new PlayerData(
                      PlayerData.AbilityType.NONE, 0, 0, 0, 0, PlayerData.SkinType.HUMAN),
                  new LevelSaveData[] {
                    new LevelSaveData(0, 0, true), new LevelSaveData(0, 0, false)
                  },
                  0));
      file.writeString(savestring, true);
    }
    saveData = json.fromJson(SaveData.class, file);
  }
Example #7
0
 public static void readExample() {
   Levels levels = json.fromJson(Levels.class, Gdx.files.internal(PATH));
   System.out.println("read:");
   System.out.println(json.prettyPrint(levels));
 }
Example #8
0
 private void prettyPrint(Object object, StringBuilder buffer, int indent, int singleLineColumns) {
   if (object instanceof OrderedMap) {
     OrderedMap<String, ?> map = (OrderedMap) object;
     if (map.size == 0) {
       buffer.append("{}");
     } else {
       boolean newLines = !isFlat(map);
       int start = buffer.length();
       outer:
       while (true) {
         buffer.append(newLines ? "{\n" : "{ ");
         int i = 0;
         for (String key : map.orderedKeys()) {
           if (newLines) indent(indent, buffer);
           buffer.append(outputType.quoteName(key));
           buffer.append(": ");
           prettyPrint(map.get(key), buffer, indent + 1, singleLineColumns);
           if (i++ < map.size - 1) buffer.append(",");
           buffer.append(newLines ? '\n' : ' ');
           if (!newLines && buffer.length() - start > singleLineColumns) {
             buffer.setLength(start);
             newLines = true;
             continue outer;
           }
         }
         break;
       }
       if (newLines) indent(indent - 1, buffer);
       buffer.append('}');
     }
   } else if (object instanceof Array) {
     Array array = (Array) object;
     if (array.size == 0) {
       buffer.append("[]");
     } else {
       boolean newLines = !isFlat(array);
       int start = buffer.length();
       outer:
       while (true) {
         buffer.append(newLines ? "[\n" : "[ ");
         for (int i = 0, n = array.size; i < n; i++) {
           if (newLines) indent(indent, buffer);
           prettyPrint(array.get(i), buffer, indent + 1, singleLineColumns);
           if (i < array.size - 1) buffer.append(",");
           buffer.append(newLines ? '\n' : ' ');
           if (!newLines && buffer.length() - start > singleLineColumns) {
             buffer.setLength(start);
             newLines = true;
             continue outer;
           }
         }
         break;
       }
       if (newLines) indent(indent - 1, buffer);
       buffer.append(']');
     }
   } else if (object instanceof String) {
     buffer.append(outputType.quoteValue((String) object));
   } else if (object instanceof Float) {
     Float floatValue = (Float) object;
     int intValue = floatValue.intValue();
     buffer.append(floatValue - intValue == 0 ? intValue : object);
   } else if (object instanceof Boolean) {
     buffer.append(object);
   } else if (object == null) {
     buffer.append("null");
   } else throw new SerializationException("Unknown object type: " + object.getClass());
 }
Example #9
0
 public String prettyPrint(String json, int singleLineColumns) {
   StringBuilder buffer = new StringBuilder(512);
   prettyPrint(new JsonReader().parse(json), buffer, 0, singleLineColumns);
   return buffer.toString();
 }
Example #10
0
 /** used to quickly create a Json File via stdout */
 public void outputAsJson() {
   Json j = new Json(OutputType.json);
   System.out.println(j.prettyPrint(this));
 }
 public void createTemplateWeapon() {
   Json json = new Json();
   json.setUsePrototypes(false);
   FileHandle file = Gdx.files.local("templateWeapon.json");
   file.writeString(json.prettyPrint(new WeaponConfig()), false);
 }