Java Code Examples for com.caucho.hessian.io.AbstractHessianOutput#writeObject()

The following examples show how to use com.caucho.hessian.io.AbstractHessianOutput#writeObject() . 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: WeakReferenceSerializer.java    From AutoLoadCache with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }
    @SuppressWarnings("unchecked")
    WeakReference<Object> data = (WeakReference<Object>) obj;

    int refV = out.writeObjectBegin(WeakReference.class.getName());

    if (refV == -1) {
        out.writeInt(1);
        out.writeString("ref");
        out.writeObjectBegin(WeakReference.class.getName());
    }
    if (data != null) {
        Object ref = data.get();
        if (null != ref) {
            out.writeObject(ref);
        } else {
            out.writeNull();
        }
    } else {
        out.writeNull();
    }
}
 
Example 2
Source File: Java8TimeSerializer.java    From jvm-sandbox-repeater with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out)
        throws IOException {
    if (obj == null) {
        out.writeNull();
        return;
    }
    T handle;
    try {
        Constructor<T> constructor = this.handleType.getConstructor(Object.class);
        handle = constructor.newInstance(obj);
    } catch (Exception e) {
        throw new RuntimeException("the class :" + this.handleType.getName() + " construct failed:" + e.getMessage(), e);
    }
    out.writeObject(handle);
}
 
Example 3
Source File: LocalTimeSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new LocalTimeHandle((LocalTime) obj));
    }
}
 
Example 4
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object2_() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  out.writeObject(new A());
  out.close();
  return bos.toByteArray();
}
 
Example 5
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field) throws IOException {
    Object value = null;

    try {
        value = field.get(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeObject(value);
}
 
Example 6
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object5() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  Map<String, String> map = new HashMap<>();
  map.put("test", "test");
  map.put("foo", "foo");
  out.writeObject(map);
  out.close();
  return bos.toByteArray();
}
 
Example 7
Source File: YearSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new YearHandle((Year) obj));
    }
}
 
Example 8
Source File: HessianSerializerUtils.java    From zkdoctor with Apache License 2.0 5 votes vote down vote up
/**
 * 将对象序列化为字节数组
 *
 * @param obj 待序列化对象
 * @return
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream ops = new ByteArrayOutputStream();
    AbstractHessianOutput out = new Hessian2Output(ops);
    out.setSerializerFactory(new SerializerFactory());
    try {
        out.writeObject(obj);
        out.close();
    } catch (IOException e) {
        LOGGER.error("Hessian serialize failed.", e);
        throw new RuntimeException("Hessian serialize failed");
    }
    return ops.toByteArray();
}
 
Example 9
Source File: ZoneOffsetSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZoneOffsetHandle((ZoneOffset) obj));
    }
}
 
Example 10
Source File: MyListSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
protected void writeObject10(Object obj, AbstractHessianOutput out) throws IOException {
    for (int i = 0; i < _fields.length; i++) {
        Field field = _fields[i];
        out.writeString(field.getName());
        _fieldSerializers[i].serialize(out, obj, field);
    }

    // writeList 内容
    out.writeString("_list_content");
    ArrayList list = new ArrayList((Collection) obj);
    out.writeObject(list);
}
 
Example 11
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object6_() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  Map<String, String> map = new HashMap<>();
  map.put("test", "test");
  out.writeObject(map);
  out.close();
  return bos.toByteArray();
}
 
Example 12
Source File: MonthDaySerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new MonthDayHandle((MonthDay) obj));
    }
}
 
Example 13
Source File: ZoneIdSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZoneIdHandle((ZoneId) obj));
    }
}
 
Example 14
Source File: DurationSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new DurationHandle((Duration) obj));
    }
}
 
Example 15
Source File: HessianSerialization.java    From learnjavabug with MIT License 5 votes vote down vote up
private static byte[] object3() throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  AbstractHessianOutput out = new HessianOutput(bos);
  NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory();
  sf.setAllowNonSerializable(true);
  out.setSerializerFactory(sf);
  out.writeObject(new B(new A()));
  out.close();
  return bos.toByteArray();
}
 
Example 16
Source File: OffsetTimeSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new OffsetTimeHandle((OffsetTime) obj));
    }
}
 
Example 17
Source File: YearMonthSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new YearMonthHandle((YearMonth) obj));
    }
}
 
Example 18
Source File: OptionalSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new OptionalHandle((Optional<?>) obj));
    }
}
 
Example 19
Source File: ZonedDateTimeSerializer.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (obj == null) {
        out.writeNull();
    } else {
        out.writeObject(new ZonedDateTimeHandle((ZonedDateTime) obj));
    }
}
 
Example 20
Source File: GenericObjectSerializer.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
void serialize(AbstractHessianOutput out, GenericObject obj, String field)
    throws IOException {
    Object value = obj.getField(field);
    out.writeObject(value);
}