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

net.dongliu.prettypb.runtime.code.object.EnumProtoFieldInfo Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.runtime.code.object;

import net.dongliu.prettypb.runtime.ExtensionRegistry;
import net.dongliu.prettypb.runtime.code.ProtoBufReader;
import net.dongliu.prettypb.runtime.include.ProtoField;
import net.dongliu.prettypb.runtime.include.ProtoType;
import net.dongliu.prettypb.runtime.code.ProtoBufWriter;
import net.dongliu.prettypb.runtime.code.ObjectProtoFieldInfo;
import net.dongliu.prettypb.runtime.utils.ProtoUtils;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @author Dong Liu
 */
public class EnumProtoFieldInfo extends ObjectProtoFieldInfo {

    public EnumProtoFieldInfo(Field field, Field helperField, ProtoField protoField) {
        super(field, helperField, protoField);
    }

    @Override
    protected void serializeFieldInternal(Object obj, ProtoBufWriter protoBufWriter)
            throws IOException {
        Enum value = (Enum) obj;
        protoBufWriter.encodeTag(ProtoType.Enum, idx());
        protoBufWriter.writeVarInt(ProtoUtils.getEnumValue(value));
    }

    @Override
    protected Object deSerializeFieldInternal(ProtoBufReader protoBufReader,
                                              ExtensionRegistry extensionRegistry)
            throws IOException {
        int value = protoBufReader.readVarInt();
        return ProtoUtils.parseEnum(clazz, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy