Java Code Examples for com.badlogic.gdx.utils.Json#setIgnoreUnknownFields()

The following examples show how to use com.badlogic.gdx.utils.Json#setIgnoreUnknownFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonProcessor.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
static <R> R process(Class<?> wantedType, String jsonString) {
    Json json = new Json();
    json.setIgnoreUnknownFields(true);
    json.setTypeName(null);
    R result = null;
    if (ClassReflection.isAssignableFrom(List.class, wantedType)
            || ClassReflection.isAssignableFrom(Map.class, wantedType)) {
        if (wantedType == List.class) {
            wantedType = ArrayList.class;
        } else if (wantedType == Map.class) {
            wantedType = HashMap.class;
        }
        json.setDefaultSerializer(new JsonListMapDeserializer(HashMap.class));
        result = (R) json.fromJson(wantedType, jsonString);
    } else {
        result = (R) json.fromJson(wantedType, jsonString);
    }
    return result;
}
 
Example 2
Source File: Config.java    From beatoraja with GNU General Public License v3.0 6 votes vote down vote up
public static Config read() {
	Config config = null;
	if (Files.exists(MainController.configpath)) {
		Json json = new Json();
		json.setIgnoreUnknownFields(true);
		try (FileReader reader = new FileReader(MainController.configpath.toFile())) {
			config = json.fromJson(Config.class, reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	if(config == null) {
		config = new Config();
	}
	config.validate();

	PlayerConfig.init(config);

	return config;
}
 
Example 3
Source File: PlayDataAccessor.java    From beatoraja with GNU General Public License v3.0 6 votes vote down vote up
/**
 * リプレイデータを読み込む
 * 
 * @param model
 *            対象のBMS
 * @param lnmode
 *            LNモード
 * @return リプレイデータ
 */
public ReplayData readReplayData(BMSModel model, int lnmode, int index) {
	if (existsReplayData(model, lnmode, index)) {
		Json json = new Json();
		json.setIgnoreUnknownFields(true);
		try {
			String path = this.getReplayDataFilePath(model, lnmode, index);
			ReplayData result = null;
			if (Files.exists(Paths.get(path + ".brd"))) {
				result =  json.fromJson(ReplayData.class, new BufferedInputStream(
						new GZIPInputStream(Files.newInputStream(Paths.get(path + ".brd")))));
			}
			if(result != null && result.validate()) {
				return result;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}
 
Example 4
Source File: JsonDataModifier.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
/**
 * Returns modified json data.
 *
 * @param oldJsonData Old data as json string.
 * @return New data as json string
 */
@SuppressWarnings("unchecked")
public String modify(String oldJsonData) {
    T oldData = JsonProcessor.process(wantedType, oldJsonData);
    T newData = (T) transactionFunction.apply(oldData);
    Json json = new Json();
    json.setTypeName(null);
    json.setQuoteLongValues(true);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    return json.toJson(newData, wantedType);
}
 
Example 5
Source File: MapConverter.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
public <T> T convert(Map<String, Object> map, Class<T> wantedType) {
    try {
        String jsonString = new Json().toJson(map);
        Json json = new Json();
        json.setIgnoreUnknownFields(true);
        json.setTypeName(null);
        json.setQuoteLongValues(true);
        return json.fromJson(wantedType, jsonString);
    } catch (Exception e) {
        GdxFIRLogger.error("Can't deserialize Map to " + wantedType.getSimpleName(), e);
        return null;
    }
}
 
Example 6
Source File: MapConverter.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Map<String, Object> unConvert(Object object) {
    try {
        String jsonString = new Json().toJson(object);
        Json json = new Json();
        json.setIgnoreUnknownFields(true);
        json.setTypeName(null);
        json.setQuoteLongValues(true);
        return json.fromJson(HashMap.class, jsonString);
    } catch (Exception e) {
        GdxFIRLogger.error("Can't serialize " + object.getClass().getSimpleName() + " to Map.", e);
        return null;
    }
}
 
Example 7
Source File: NSDictionaryHelper.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static NSDictionary toNSDictionary(Object object) {
    if (object instanceof Map) {
        return toNSDictionary((Map) object);
    } else {
        String objectJsonData = new Json().toJson(object);
        Json json = new Json();
        json.setIgnoreUnknownFields(true);
        Map objectMap = json.fromJson(HashMap.class, objectJsonData);
        return toNSDictionary(objectMap);
    }
}
 
Example 8
Source File: MapTransformer.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
static String mapToJSON(Map<String, Object> map) {
    Json json = new Json();
    json.setTypeName(null);
    json.setQuoteLongValues(true);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    return json.toJson(map, HashMap.class);
}
 
Example 9
Source File: PlayDataAccessor.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
/**
 * コースリプレイデータを読み込む
 * 
 * @param hash
 *            対象のBMSハッシュ群
 * @param lnmode
 *            LNモード
 * @return リプレイデータ
 */
public ReplayData[] readReplayData(String[] hash, boolean ln, int lnmode, int index,
		CourseData.CourseDataConstraint[] constraint) {
	if (existsReplayData(hash, ln, lnmode, index, constraint)) {
		Json json = new Json();
		json.setIgnoreUnknownFields(true);
		try {
			String path = this.getReplayDataFilePath(hash, ln, lnmode, index, constraint);
			ReplayData[] result = null;
			if (Files.exists(Paths.get(path + ".brd"))) {
				result = json.fromJson(ReplayData[].class, new BufferedInputStream(
						new GZIPInputStream(Files.newInputStream(Paths.get(path + ".brd")))));
			}
			if(result != null) {
				for(ReplayData rd : result) {
					if(rd == null || !rd.validate()) {
						return null;
					}
				}
				return result;
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}
 
Example 10
Source File: StringGenerator.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
public static String dataToString(Object object) {
    if (isPrimitiveType(object))
        return object.toString();
    Json json = new Json();
    json.setTypeName(null);
    json.setQuoteLongValues(false);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    return json.toJson(object);
}
 
Example 11
Source File: PlayerConfig.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
public static PlayerConfig readPlayerConfig(String playerpath, String playerid) {
	PlayerConfig player = new PlayerConfig();
	try (FileReader reader = new FileReader(Paths.get(playerpath + "/" + playerid + "/config.json").toFile())) {
		Json json = new Json();
		json.setIgnoreUnknownFields(true);
		player = json.fromJson(PlayerConfig.class, reader);
		player.setId(playerid);
		player.validate();
	} catch(Throwable e) {
		e.printStackTrace();
	}
	return player;
}
 
Example 12
Source File: JsonSkinSerializer.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
private void includeArray(Json json, JsonValue jsonValue, Class cls, ArrayList<T> items) {
	Json subJson = new Json();
	subJson.setIgnoreUnknownFields(true);
	File file = pathGetter.apply(path.getParent().toString() + "/" + jsonValue.get("include").asString());
	if (file.exists()) {
		setSerializers(subJson, this.options, file.toPath());
		try {
			T[] array = (T[])subJson.fromJson(cls, new FileReader(file));
			Collections.addAll(items, array);
		} catch (FileNotFoundException e) {
		}
	}
}
 
Example 13
Source File: JSONSkinLoader.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
public SkinHeader loadHeader(Path p) {
	serializer = new JsonSkinSerializer(lua, path -> getPath(path, filemap));
	SkinHeader header = null;
	try {
		Json json = new Json();
		json.setIgnoreUnknownFields(true);
		serializer.setSerializers(json, null, p);
		sk = json.fromJson(JsonSkin.Skin.class, new FileReader(p.toFile()));
		header = loadJsonSkinHeader(sk, p);
	} catch (FileNotFoundException e) {
		Logger.getGlobal().severe("JSONスキンファイルが見つかりません : " + p.toString());
	}
	return header;
}
 
Example 14
Source File: CocoStudioUIEditor.java    From cocos-ui-libgdx with Apache License 2.0 5 votes vote down vote up
public static List<String> getResources(FileHandle jsonFile) {
    dirName = jsonFile.parent().toString();

    if (!dirName.equals("")) {
        dirName += File.separator;
    }
    String json = jsonFile.readString("utf-8");
    Json jj = new Json();
    jj.setIgnoreUnknownFields(true);
    CCExport export = jj.fromJson(CCExport.class, json);
    return export.getContent().getContent().getUsedResources();
}
 
Example 15
Source File: WorldSerialization.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Load the world description in 'world.json'.
 * 
 * @throws IOException
 */
public void loadWorldDesc() throws IOException {

	String worldFilename = EngineAssetManager.WORLD_FILENAME;

	if (!EngineAssetManager.getInstance().getModelFile(worldFilename).exists()) {

		// Search the world file with ".json" ext if not found.
		worldFilename = EngineAssetManager.WORLD_FILENAME + ".json";

		if (!EngineAssetManager.getInstance().getModelFile(worldFilename).exists()) {
			EngineLogger.error("ERROR LOADING WORLD: world file not found.");
			w.dispose();
			throw new IOException("ERROR LOADING WORLD: world file not found.");
		}
	}

	JsonValue root = new JsonReader()
			.parse(EngineAssetManager.getInstance().getModelFile(worldFilename).reader("UTF-8"));

	Json json = new BladeJson(w, Mode.MODEL);
	json.setIgnoreUnknownFields(true);

	int width = json.readValue("width", Integer.class, root);
	int height = json.readValue("height", Integer.class, root);

	// We know the world width, so we can set the scale
	EngineAssetManager.getInstance().setScale(width, height);
	float scale = EngineAssetManager.getInstance().getScale();

	w.setWidth((int) (width * scale));
	w.setHeight((int) (height * scale));
	w.setInitChapter(json.readValue("initChapter", String.class, root));
	w.getVerbManager().read(json, root);
	w.getI18N().loadWorld(EngineAssetManager.MODEL_DIR + EngineAssetManager.WORLD_FILENAME);
}
 
Example 16
Source File: CocoStudioUIEditor.java    From cocos-ui-libgdx with Apache License 2.0 4 votes vote down vote up
/**
 * @param jsonFile     ui编辑成生成的json文件
 * @param textureAtlas 资源文件,传入 null表示使用小文件方式加载图片.
 * @param ttfs         字体文件集合
 * @param bitmapFonts  自定义字体文件集合
 * @param defaultFont  默认ttf字体文件
 */
public CocoStudioUIEditor(FileHandle jsonFile,
                          Map<String, FileHandle> ttfs, Map<String, BitmapFont> bitmapFonts,
                          FileHandle defaultFont, Collection<TextureAtlas> textureAtlas) {
    this.textureAtlas = textureAtlas;
    this.ttfs = ttfs;
    this.bitmapFonts = bitmapFonts;
    this.defaultFont = defaultFont;
    parsers = new HashMap<>();

    addParser(new CCButton());
    addParser(new CCCheckBox());
    addParser(new CCImageView());
    addParser(new CCLabel());
    addParser(new CCLabelBMFont());
    addParser(new CCPanel());
    addParser(new CCScrollView());
    addParser(new CCTextField());
    addParser(new CCLoadingBar());
    addParser(new CCTextAtlas());

    addParser(new CCLayer());

    addParser(new CCLabelAtlas());
    addParser(new CCSpriteView());
    addParser(new CCNode());

    addParser(new CCSlider());

    addParser(new CCParticle());
    addParser(new CCProjectNode());
    addParser(new CCPageView());

    addParser(new CCTImageView());

    actors = new HashMap<String, Array<Actor>>();
    actionActors = new HashMap<Integer, Actor>();

    //animations = new HashMap<String, Map<Actor, Action>>();

    actorActionMap = new HashMap<Actor, Action>();

    dirName = jsonFile.parent().toString();

    if (!dirName.equals("")) {
        dirName += File.separator;
    }
    String json = jsonFile.readString("utf-8");
    Json jj = new Json();
    jj.setIgnoreUnknownFields(true);
    export = jj.fromJson(CCExport.class, json);
}
 
Example 17
Source File: WorldSerialization.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Loads a JSON chapter file.
 * 
 * @param chapterName filename without path and extension.
 * @param scene       the init scene. null to use the chapter defined init
 *                    scene.
 * @param initScene   false only when it comes from loading a saved game.
 * @throws IOException
 */
public void loadChapter(String chapterName, String scene, boolean initScene) throws IOException {
	if (!w.isDisposed())
		w.dispose();

	long initTime = System.currentTimeMillis();

	if (chapterName == null)
		chapterName = w.getInitChapter();

	w.setChapter(chapterName);

	if (EngineAssetManager.getInstance().getModelFile(chapterName + EngineAssetManager.CHAPTER_EXT).exists()) {

		JsonValue root = new JsonReader().parse(EngineAssetManager.getInstance()
				.getModelFile(chapterName + EngineAssetManager.CHAPTER_EXT).reader("UTF-8"));

		Json json = new BladeJson(w, Mode.MODEL, initScene);
		json.setIgnoreUnknownFields(true);

		read(json, root);

		if (scene == null)
			w.setCurrentScene(w.getScenes().get(w.getInitScene()), initScene, null);
		else
			w.setCurrentScene(w.getScenes().get(scene), initScene, null);

		w.getI18N().loadChapter(EngineAssetManager.MODEL_DIR + chapterName);

		w.getCustomProperties().put(WorldProperties.CURRENT_CHAPTER.toString(), chapterName);
		w.getCustomProperties().put(WorldProperties.PLATFORM.toString(), Gdx.app.getType().toString());
	} else {
		EngineLogger.error(
				"ERROR LOADING CHAPTER: " + chapterName + EngineAssetManager.CHAPTER_EXT + " doesn't exists.");
		w.dispose();
		throw new IOException(
				"ERROR LOADING CHAPTER: " + chapterName + EngineAssetManager.CHAPTER_EXT + " doesn't exists.");
	}

	EngineLogger.debug("MODEL LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
}
 
Example 18
Source File: WorldSerialization.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
public void loadGameState(FileHandle savedFile) throws IOException {
	EngineLogger.debug("LOADING GAME STATE");

	if (savedFile.exists()) {

		JsonValue root = new JsonReader().parse(savedFile.reader("UTF-8"));

		Json json = new BladeJson(w, Mode.STATE);
		json.setIgnoreUnknownFields(true);

		read(json, root);

	} else {
		throw new IOException("LOADGAMESTATE: no saved game exists");
	}
}