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

The following examples show how to use com.caucho.hessian.io.AbstractHessianOutput#writeObjectBegin() . 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: GenericClassSerializer.java    From sofa-hessian with Apache License 2.0 6 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    GenericClass genericClass = (GenericClass) obj;

    if (genericClass == null) {
        out.writeNull();
    } else if (out.addRef(obj)) {
        return;
    } else {
        int ref = out.writeObjectBegin("java.lang.Class");

        if (ref < -1) {
            out.writeString("name");
            out.writeString(genericClass.getClazzName());
            out.writeMapEnd();
        } else {
            if (ref == -1) {
                out.writeInt(1);
                out.writeString("name");
                out.writeObjectBegin("java.lang.Class");
            }

            out.writeString(genericClass.getClazzName());
        }
    }
}
 
Example 2
Source File: MyListSerializer.java    From sofa-hessian 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;
    }

    Class cl = obj.getClass();
    int ref = out.writeObjectBegin(cl.getName());

    if (ref < -1) {
        writeObject10(obj, out);
    } else {
        if (ref == -1) {
            writeDefinition20(out);
            out.writeObjectBegin(cl.getName());
        }

        writeInstance(obj, out);
    }
}
 
Example 3
Source File: SoftReferenceSerializer.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")
    SoftReference<Object> data = (SoftReference<Object>) obj;

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

    if (refV == -1) {
        out.writeInt(1);
        out.writeString("ref");
        out.writeObjectBegin(SoftReference.class.getName());
    }
    if (data != null) {
        Object ref = data.get();
        if (null != ref) {
            out.writeObject(ref);
        } else {
            out.writeNull();
        }
    } else {
        out.writeNull();
    }
}
 
Example 4
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 5
Source File: LocalDateTimeSerializer.java    From jvm-sandbox-repeater 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 {
        Class cl = obj.getClass();

        if (out.addRef(obj)) {
            return;
        }
        // ref 返回-2 便是开始写Map
        int ref = out.writeObjectBegin(cl.getName());

        if (ref < -1) {
            out.writeString("value");
            out.writeUTCDate(((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli());
            out.writeMapEnd();
        } else {
            if (ref == -1) {
                out.writeInt(1);
                out.writeString("value");
                out.writeObjectBegin(cl.getName());
            }
            out.writeUTCDate(((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli());
        }
    }
}
 
Example 6
Source File: TranscribableSerialiser.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object obj, final AbstractHessianOutput out) throws IOException {
	try {
		if (out.addRef(obj)) {
			return;
		}

           Transcribable transcribable = (Transcribable) obj;
           ServiceVersion serviceVersion = transcribable.getServiceVersion();
		Class clazz = obj.getClass();

           String classNameToWrite = clazz.getName();
           // if we're talking to an older version we need to migrate vMajor to vMajor_Minor
           if (!transcriptionParams.contains(TranscribableParams.MajorOnlyPackageNaming)) {
               // look for vMajor
               classNameToWrite = ClassnameCompatibilityMapper.toMajorMinorPackaging(clazz, serviceVersion);
           }
		int ref = out.writeObjectBegin(classNameToWrite);

           // true if the object has already been defined
		if (ref >= 0) {
			transcribe(out,transcribable, client);
		}
		else  {
			Parameter[] parameters =  transcribable.getParameters();
			out.writeInt(parameters.length);
			for (int i=0; i < parameters.length; i++) {
				out.writeString(parameters[i].getName());
			}
			out.writeObjectBegin(classNameToWrite);
			transcribe(out,transcribable, client);
		}
	}
	catch (Exception e) {
		throw new IOException(e);
	}


}
 
Example 7
Source File: TranscribableEnumSerializer.java    From cougar with Apache License 2.0 5 votes vote down vote up
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
    if (out.addRef(obj)) {
        return;
    }

    Class cl = obj.getClass();

    String name = null;
    try {
        name = (String) _name.invoke(obj, (Object[]) null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    // if we're talking to an older version we need to migrate vMajor to vMajor_Minor
    String className = cl.getName();
    if (!transcriptionParams.contains(TranscribableParams.MajorOnlyPackageNaming)) {
        className = ClassnameCompatibilityMapper.toMajorMinorPackaging(cl, _serviceVersion);
    }
    int ref = out.writeObjectBegin(className);

    if (ref < -1) {
        out.writeString("name");
        out.writeString(name);
        out.writeMapEnd();
    }
    else {
        if (ref == -1) {
            out.writeClassFieldLength(1);
            out.writeString("name");
            out.writeObjectBegin(className);
        }

        out.writeString(name);
    }
}