本文整理汇总了Java中jodd.json.JsonContext类的典型用法代码示例。如果您正苦于以下问题:Java JsonContext类的具体用法?Java JsonContext怎么用?Java JsonContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonContext类属于jodd.json包,在下文中一共展示了JsonContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serializeMap
import jodd.json.JsonContext; //导入依赖的package包/类
private static boolean serializeMap(final JsonContext jsonContext, Map<?, ?> map) {
if (jsonContext.pushValue(map)) {
// prevent circular dependencies
return false;
}
jsonContext.writeOpenObject();
boolean notfirst = false;
for (Map.Entry<?, ?> entry : map.entrySet()) {
final Object value = entry.getValue();
if (value == null) {
continue;
}
final Object key = entry.getKey();
jsonContext.pushName(key != null ? key.toString() : null, notfirst);
jsonContext.serialize(value);
if (!notfirst && jsonContext.isNamePopped()) {
notfirst = true;
}
}
jsonContext.writeCloseObject();
jsonContext.popValue();
return true;
}
示例2: serializeValue
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public void serializeValue(JsonContext jsonContext, Map<?, ?> map) {
jsonContext.writeOpenObject();
int count = 0;
Path currentPath = jsonContext.getPath();
for (Map.Entry<?, ?> entry : map.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
count = serializeKeyValue(jsonContext, currentPath, key, value, count);
}
jsonContext.writeCloseObject();
}
示例3: serializeValue
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public void serializeValue(JsonContext jsonContext, Object array) {
jsonContext.writeOpenArray();
int length = getLength((K[]) array);
int count = 0;
for (int i = 0; i < length; i++) {
if (count > 0) {
jsonContext.writeComma();
}
if (jsonContext.serialize(get((K[]) array, i))) {
count++;
}
}
jsonContext.writeCloseArray();
}
示例4: serializeValue
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public void serializeValue(JsonContext jsonContext, Iterable iterable) {
jsonContext.writeOpenArray();
int count = 0;
for (Object element : iterable) {
if (count > 0) {
jsonContext.writeComma();
}
if (jsonContext.serialize(element)) {
count++;
}
}
jsonContext.writeCloseArray();
}
示例5: serializeObject
import jodd.json.JsonContext; //导入依赖的package包/类
private static boolean serializeObject(final JsonContext jsonContext, final Object source) {
if (jsonContext.pushValue(source)) {
// prevent circular dependencies
return false;
}
InternalJsonContext internalJsonContext = (InternalJsonContext) jsonContext;
internalJsonContext.writeOpenObject();
final PropertyDescriptor[] propertyDescriptors = ClassIntrospector.lookup(source.getClass()).getAllPropertyDescriptors();
boolean notfirst = false;
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
final String propertyName = propertyDescriptor.getName();
if (internalJsonContext.isExcluded(source, propertyName)) {
continue;
}
final Getter getter = propertyDescriptor.getGetter(false);
if (getter == null) {
continue;
}
final Object value;
try {
value = getter.invokeGetter(source);
} catch (InvocationTargetException | IllegalAccessException ex) {
throw new JsonException(ex);
}
if (value == null) {
continue;
}
internalJsonContext.pushName(propertyName, notfirst);
internalJsonContext.serialize(value);
if (!notfirst && internalJsonContext.isNamePopped()) {
notfirst = true;
}
}
internalJsonContext.writeCloseObject();
jsonContext.popValue();
return true;
}
示例6: serializeLongArray
import jodd.json.JsonContext; //导入依赖的package包/类
private static boolean serializeLongArray(JsonContext jsonContext, long[] array) {
jsonContext.writeOpenArray();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
jsonContext.writeComma();
}
jsonContext.write(new StringBuilder().append('\"').append(array[i]).append('\"'));
}
jsonContext.writeCloseArray();
return true;
}
示例7: addSerializer
import jodd.json.JsonContext; //导入依赖的package包/类
public static final <T> void addSerializer(JsonSerializer mapper, Class<T> type,
BiConsumer<T, JsonContext> consumer) {
mapper.withSerializer(type, new TypeJsonSerializer<T>() {
@Override
public final boolean serialize(JsonContext ctx, T value) {
consumer.accept(value, ctx);
return true;
}
});
}
示例8: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public boolean serialize(JsonContext jsonContext, double[] array) {
jsonContext.writeOpenArray();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
jsonContext.writeComma();
}
jsonContext.write(Double.toString(array[i]));
}
jsonContext.writeCloseArray();
return true;
}
示例9: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public boolean serialize(JsonContext jsonContext, int[] array) {
jsonContext.writeOpenArray();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
jsonContext.writeComma();
}
jsonContext.write(Integer.toString(array[i]));
}
jsonContext.writeCloseArray();
return true;
}
示例10: serializeValue
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public void serializeValue(final JsonContext jsonContext, Object value) {
jsonContext.writeOpenObject();
BeanSerializer beanVisitor = new BeanSerializer(jsonContext, value);
beanVisitor.serialize();
jsonContext.writeCloseObject();
}
示例11: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public boolean serialize(JsonContext jsonContext, File file) {
switch (serializationType) {
case PATH:
jsonContext.writeString(file.getAbsolutePath());
break;
case NAME:
jsonContext.writeString(file.getName());
break;
case CONTENT: {
byte[] bytes;
try {
bytes = FileUtil.readBytes(file);
}
catch (IOException e) {
throw new JsonException("Unable to read files content", e);
}
String encoded = Base64.encodeToString(bytes);
jsonContext.writeString(encoded);
}
break;
default:
throw new JsonException("Invalid type");
}
return true;
}
示例12: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public boolean serialize(JsonContext jsonContext, float[] array) {
jsonContext.writeOpenArray();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
jsonContext.writeComma();
}
jsonContext.write(Float.toString(array[i]));
}
jsonContext.writeCloseArray();
return true;
}
示例13: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public boolean serialize(JsonContext jsonContext, long[] array) {
jsonContext.writeOpenArray();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
jsonContext.writeComma();
}
jsonContext.write(Long.toString(array[i]));
}
jsonContext.writeCloseArray();
return true;
}
示例14: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
@Override
public boolean serialize(JsonContext jsonContext, boolean[] array) {
jsonContext.writeOpenArray();
for (int i = 0; i < array.length; i++) {
if (i > 0) {
jsonContext.writeComma();
}
jsonContext.write(Boolean.toString(array[i]));
}
jsonContext.writeCloseArray();
return true;
}
示例15: serialize
import jodd.json.JsonContext; //导入依赖的package包/类
/**
* Detects circular dependencies and pushes value as current
* type context.
*/
public final boolean serialize(JsonContext jsonContext, T value) {
if (jsonContext.pushValue(value)) {
// prevent circular dependencies
return false;
}
serializeValue(jsonContext, value);
jsonContext.popValue();
return true;
}