Skip to content

Commit

Permalink
JsonUtil Helper Class (#178)
Browse files Browse the repository at this point in the history
* Initial implementation of JsonUtil

* Switching over to using JsonUtil

* Warn on unknown fields instead of throwing.

* Improving error logging for failed deserialization

* Removing instances of newing Json directly

* Replace tabs with spaces

* Typo

* Updating Options class to use JsonUtil defaultValueSupplier

* 💄

* Shortening log tag

* Missed a Game.fromJson usage

* Comment for clarification
  • Loading branch information
joshuaskelly authored Nov 17, 2020
1 parent 2a83f65 commit 25a7ee8
Show file tree
Hide file tree
Showing 22 changed files with 586 additions and 536 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.interrupt.managers.EntityManager;
import com.interrupt.managers.MonsterManager;
import com.interrupt.managers.StringManager;
import com.interrupt.utils.JsonUtil;
import com.noise.PerlinNoise;

import javax.swing.*;
Expand Down Expand Up @@ -1310,14 +1311,14 @@ else if(v.vertex == ControlVertex.slopeSW) {

if(Gdx.input.isKeyPressed(Input.Keys.ALT_LEFT)) {
// Make a copy
Entity copy = Game.fromJson(Editor.selection.picked.getClass(), Game.toJson(Editor.selection.picked));
Entity copy = JsonUtil.fromJson(Editor.selection.picked.getClass(), JsonUtil.toJson(Editor.selection.picked));
level.entities.add(copy);

pickEntity(copy);

Array<Entity> copies = new Array<Entity>();
for(Entity selected : Editor.selection.selected) {
Entity newCopy = Game.fromJson(selected.getClass(), Game.toJson(selected));
Entity newCopy = JsonUtil.fromJson(selected.getClass(), JsonUtil.toJson(selected));
level.entities.add(newCopy);
copies.add(newCopy);
}
Expand Down
3 changes: 2 additions & 1 deletion DelvEdit/src/com/interrupt/dungeoneer/editor/EditorArt.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.interrupt.dungeoneer.gfx.Tesselator;
import com.interrupt.dungeoneer.gfx.TextureAtlas;
import com.interrupt.dungeoneer.ui.UiSkin;
import com.interrupt.utils.JsonUtil;

public class EditorArt {

Expand All @@ -20,7 +21,7 @@ public class EditorArt {

public static void initAtlases() {
FileHandle itemFile = Game.getInternal("data/spritesheets.dat");
TextureAtlas[] atlases = Game.fromJson(TextureAtlas[].class, itemFile);
TextureAtlas[] atlases = JsonUtil.fromJson(TextureAtlas[].class, itemFile);

atlasList = new String[atlases.length + 1];
int curAtlas = 0;
Expand Down
23 changes: 11 additions & 12 deletions DelvEdit/src/com/interrupt/dungeoneer/editor/EditorClipboard.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.interrupt.dungeoneer.editor;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.*;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Clipboard;
import com.interrupt.dungeoneer.editor.selection.TileSelectionInfo;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.game.Game;
import com.interrupt.dungeoneer.tiles.Tile;
import com.interrupt.utils.JsonUtil;

public class EditorClipboard {
public Array<Entity> entities = new Array<>();
Expand Down Expand Up @@ -48,14 +49,11 @@ public static void copy() {

// Serialize to system clipboard.
Clipboard systemClipboard = Gdx.app.getClipboard();
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
String contents = json.prettyPrint(json.toJson(instance), 40);
systemClipboard.setContents(contents);
systemClipboard.setContents(JsonUtil.toJson(instance));
}

private static Entity copyEntity(Entity entity) {
return Game.fromJson(entity.getClass(), Game.toJson(entity));
return JsonUtil.fromJson(entity.getClass(), JsonUtil.toJson(entity));
}

/** Serialize JSON from system clipboard and add to level. */
Expand All @@ -65,19 +63,20 @@ public static void paste() {
// Deserialize from system clipboard.
try {
Clipboard systemClipboard = Gdx.app.getClipboard();
Json json = new Json();
instance = json.fromJson(EditorClipboard.class, systemClipboard.getContents());
instance = JsonUtil.fromJson(EditorClipboard.class, systemClipboard.getContents());
}
catch (Exception ignored) {}

// Try deserializing a single entity.
if (instance == null) {
try {
Clipboard systemClipboard = Gdx.app.getClipboard();
Json json = new Json();
Entity e = json.fromJson(Entity.class, systemClipboard.getContents());
Entity e = JsonUtil.fromJson(Entity.class, systemClipboard.getContents());
instance = new EditorClipboard();
instance.entities.add(e);

if (e != null) {
instance.entities.add(e);
}
} catch (Exception e) {
Gdx.app.log("Editor", e.getMessage());
}
Expand Down
27 changes: 13 additions & 14 deletions DelvEdit/src/com/interrupt/dungeoneer/editor/EditorOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,28 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.interrupt.utils.JsonUtil;

/** Editor options container class. */
public class EditorOptions {
public Array<String> recentlyOpenedFiles;

public EditorOptions() {
recentlyOpenedFiles = new Array<String>();
recentlyOpenedFiles = new Array<>();
}

public static EditorOptions fromLocalFiles() {
String path = "/save/editor.txt";
Json json = new Json();
FileHandle file = Gdx.files.local(path);
FileHandle file = Gdx.files.local(getEditorOptionsFilePath());

if (!file.exists()) {
file.writeString(json.toJson(new EditorOptions()), false);
}

return json.fromJson(EditorOptions.class, file);
return JsonUtil.fromJson(EditorOptions.class, file, ()-> {
EditorOptions eo = new EditorOptions();
JsonUtil.toJson(eo, file);
return eo;
});
}

public static void toLocalFiles(EditorOptions instance) {
String path = "/save/editor.txt";
Json json = new Json();
FileHandle file = Gdx.files.local(path);
file.writeString(json.toJson(instance), false);
JsonUtil.toJson(instance, getEditorOptionsFilePath());
}

public void save() {
Expand All @@ -40,6 +35,10 @@ public void dispose() {
Editor.options.save();
}

public static String getEditorOptionsFilePath() {
return "/save/editor.txt";
}

public void removeRecentlyOpenedFile(String path) {
recentlyOpenedFiles.removeValue(path, false);
}
Expand Down
11 changes: 2 additions & 9 deletions DelvEdit/src/com/interrupt/dungeoneer/editor/JsonViewer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.interrupt.dungeoneer.editor;

import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.entities.Group;
import com.interrupt.dungeoneer.entities.Prefab;
import com.interrupt.dungeoneer.game.Game;
import com.interrupt.utils.JsonUtil;

import javax.swing.*;

Expand Down Expand Up @@ -34,13 +32,8 @@ public JsonViewer(final Entity entity) {
((Prefab) copy).entities.clear();
}


String jsonText = Game.toJson(copy, Entity.class);
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
jsonText = json.prettyPrint(jsonText, 40);
String jsonText = JsonUtil.toJson(copy, Entity.class);
textArea.setText(jsonText);

add(textArea);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
import com.badlogic.gdx.utils.TimeUtils;
import com.interrupt.dungeoneer.editor.Editor;
import com.interrupt.dungeoneer.editor.history.EditorHistory;
import com.interrupt.dungeoneer.editor.ui.EditorUi;
import com.interrupt.dungeoneer.editor.ui.FilePicker;
import com.interrupt.dungeoneer.editor.ui.NewLevelDialog;
import com.interrupt.dungeoneer.editor.ui.SaveChangesDialog;
import com.interrupt.dungeoneer.editor.ui.WarningDialog;
import com.interrupt.dungeoneer.game.Game;
import com.interrupt.dungeoneer.editor.ui.*;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.serializers.KryoSerializer;
import com.interrupt.dungeoneer.tiles.ExitTile;
import com.interrupt.dungeoneer.tiles.Tile;
import com.interrupt.utils.JsonUtil;

import java.io.File;
import java.io.FileFilter;
Expand Down Expand Up @@ -174,7 +170,7 @@ private void saveInternal(String fileName) {

// write as json
if(fileName.endsWith(".dat")) {
Game.toJson(level, Gdx.files.absolute(fileName));
JsonUtil.toJson(level, Gdx.files.absolute(fileName));
}
else {
KryoSerializer.saveLevel(Gdx.files.absolute(fileName), level);
Expand Down Expand Up @@ -352,7 +348,7 @@ private Level loadBinFile(FileHandle file) {

/** Loads a `.dat` level file. */
private Level loadDatFile(FileHandle file) {
Level level = Game.fromJson(Level.class, file);
Level level = JsonUtil.fromJson(Level.class, file);
level.init(Level.Source.EDITOR);

return level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.interrupt.dungeoneer.editor.ui.menu.generator;

import java.io.File;
import java.io.FileFilter;
import java.util.Comparator;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.interrupt.dungeoneer.game.Game;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.generator.SectionDefinition;
import com.interrupt.utils.JsonUtil;

import java.io.File;
import java.io.FileFilter;
import java.util.Comparator;

public class GeneratorInfo {
private Array<String> themes = new Array<String>();
Expand Down Expand Up @@ -63,7 +64,7 @@ public boolean accept(File file) {
// Check for room/level definition.
FileHandle section = Game.getInternal(mod + "/generator/" + child.name() + "/section.dat");
if (section.exists()) {
SectionDefinition sectionDefinition = Game.fromJson(SectionDefinition.class, section);
SectionDefinition sectionDefinition = JsonUtil.fromJson(SectionDefinition.class, section);

if (!sectionDefinitions.contains(sectionDefinition, false)) {
sectionDefinitions.add(sectionDefinition);
Expand Down
10 changes: 7 additions & 3 deletions Dungeoneer/src/com/interrupt/dungeoneer/GameApplication.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.interrupt.dungeoneer;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.interrupt.api.steam.SteamApi;
import com.interrupt.dungeoneer.entities.Stairs;
import com.interrupt.dungeoneer.entities.triggers.TriggeredWarp;
import com.interrupt.dungeoneer.game.GameData;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.game.Options;
import com.interrupt.dungeoneer.screens.*;
import com.interrupt.utils.JsonUtil;

public class GameApplication extends Game {

Expand All @@ -36,7 +38,8 @@ public void create() {
gameManager = new GameManager(this);
Gdx.input.setInputProcessor( input );
gameManager.init();

Options.loadOptions();

mainMenuScreen = new SplashScreen();
mainScreen = new GameScreen(gameManager, input);
gameoverScreen = new GameOverScreen(gameManager);
Expand All @@ -53,6 +56,7 @@ public void createFromEditor(Level level) {
gameManager = new GameManager(this);
Gdx.input.setInputProcessor( input );
gameManager.init();
Options.loadOptions();

com.interrupt.dungeoneer.game.Game.inEditor = true;
mainMenuScreen = new SplashScreen();
Expand All @@ -79,7 +83,7 @@ public static void ShowGameOverScreen(boolean escaped) {

// Only show the ending level once!
if(escaped) {
GameData gameData = com.interrupt.dungeoneer.game.Game.fromJson(GameData.class, com.interrupt.dungeoneer.game.Game.findInternalFileInMods("data/game.dat"));
GameData gameData = JsonUtil.fromJson(GameData.class, com.interrupt.dungeoneer.game.Game.findInternalFileInMods("data/game.dat"));
Level endingLevel = gameData.endingLevel;

// Warp to the ending level, if we're not there already.
Expand Down
Loading

0 comments on commit 25a7ee8

Please sign in to comment.