All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minidev.json.reader.BeansWriterASMRemap Maven / Gradle / Ivy

Go to download

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

The newest version!
package net.minidev.json.reader;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.minidev.asm.Accessor;
import net.minidev.asm.BeansAccess;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONStyle;
import net.minidev.json.JSONUtil;

public class BeansWriterASMRemap implements JsonWriterI {
	private Map rename = new HashMap();

	public void renameField(String source, String dest) {
		rename.put(source, dest);
	}

	private String rename(String key) {
		String k2 = rename.get(key);
		if (k2 != null)
			return k2;
		return key;
	}

	public  void writeJSONString(E value, Appendable out, JSONStyle compression) throws IOException {
		try {
			Class cls = value.getClass();
			boolean needSep = false;
			@SuppressWarnings("rawtypes")
			BeansAccess fields = BeansAccess.get(cls, JSONUtil.JSON_SMART_FIELD_FILTER);
			out.append('{');
			for (Accessor field : fields.getAccessors()) {
				@SuppressWarnings("unchecked")
				Object v = fields.get(value, field.getIndex());
				if (v == null && compression.ignoreNull())
					continue;
				if (needSep)
					out.append(',');
				else
					needSep = true;
				String key = field.getName();
				key = rename(key);
				JSONObject.writeJSONKV(key, v, out, compression);
			}
			out.append('}');
		} catch (IOException e) {
			throw e;
		}
	}

}