Цитата(Pawl @ 5.6.2014, 23:54 ) | Поправьте меня, если ошибаюсь: protobuf сериализатор/десериализатор не пишется руками и не генерится? Как же он тогда получается? |
У него немного другая идеология, есть язык описания доменных обхектов, на котором ты описываешь их, а дальше он генерирует классы для нужного языка (стандартно поддерживаются Java, C++, Python, и есть сторонние реализации практически для всех остальных языков). Код | package com.example.test;
message ClientKey { required int32 id = 1; required string name = 2; optional bool flag = 3 [default = false]; }
|
Код | // Generated by the protocol buffer compiler. DO NOT EDIT! // source: test.proto
package com.example.test;
public final class Test { private Test() {} public static void registerAllExtensions( com.google.protobuf.ExtensionRegistry registry) { } public interface ClientKeyOrBuilder extends com.google.protobuf.MessageOrBuilder { // required int32 id = 1; boolean hasId(); int getId(); // required string name = 2; boolean hasName(); String getName(); // optional bool flag = 3 [default = false]; boolean hasFlag(); boolean getFlag(); } public static final class ClientKey extends com.google.protobuf.GeneratedMessage implements ClientKeyOrBuilder { // Use ClientKey.newBuilder() to construct. private ClientKey(Builder builder) { super(builder); } private ClientKey(boolean noInit) {} private static final ClientKey defaultInstance; public static ClientKey getDefaultInstance() { return defaultInstance; } public ClientKey getDefaultInstanceForType() { return defaultInstance; } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return com.example.test.Test.internal_static_com_example_test_ClientKey_descriptor; } protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { return com.example.test.Test.internal_static_com_example_test_ClientKey_fieldAccessorTable; } private int bitField0_; // required int32 id = 1; public static final int ID_FIELD_NUMBER = 1; private int id_; public boolean hasId() { return ((bitField0_ & 0x00000001) == 0x00000001); } public int getId() { return id_; } // required string name = 2; public static final int NAME_FIELD_NUMBER = 2; private java.lang.Object name_; public boolean hasName() { return ((bitField0_ & 0x00000002) == 0x00000002); } public String getName() { java.lang.Object ref = name_; if (ref instanceof String) { return (String) ref; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); if (com.google.protobuf.Internal.isValidUtf8(bs)) { name_ = s; } return s; } } private com.google.protobuf.ByteString getNameBytes() { java.lang.Object ref = name_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((String) ref); name_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } // optional bool flag = 3 [default = false]; public static final int FLAG_FIELD_NUMBER = 3; private boolean flag_; public boolean hasFlag() { return ((bitField0_ & 0x00000004) == 0x00000004); } public boolean getFlag() { return flag_; } private void initFields() { id_ = 0; name_ = ""; flag_ = false; } private byte memoizedIsInitialized = -1; public final boolean isInitialized() { byte isInitialized = memoizedIsInitialized; if (isInitialized != -1) return isInitialized == 1; if (!hasId()) { memoizedIsInitialized = 0; return false; } if (!hasName()) { memoizedIsInitialized = 0; return false; } memoizedIsInitialized = 1; return true; } public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { getSerializedSize(); if (((bitField0_ & 0x00000001) == 0x00000001)) { output.writeInt32(1, id_); } if (((bitField0_ & 0x00000002) == 0x00000002)) { output.writeBytes(2, getNameBytes()); } if (((bitField0_ & 0x00000004) == 0x00000004)) { output.writeBool(3, flag_); } getUnknownFields().writeTo(output); } private int memoizedSerializedSize = -1; public int getSerializedSize() { int size = memoizedSerializedSize; if (size != -1) return size; size = 0; if (((bitField0_ & 0x00000001) == 0x00000001)) { size += com.google.protobuf.CodedOutputStream .computeInt32Size(1, id_); } if (((bitField0_ & 0x00000002) == 0x00000002)) { size += com.google.protobuf.CodedOutputStream .computeBytesSize(2, getNameBytes()); } if (((bitField0_ & 0x00000004) == 0x00000004)) { size += com.google.protobuf.CodedOutputStream .computeBoolSize(3, flag_); } size += getUnknownFields().getSerializedSize(); memoizedSerializedSize = size; return size; } private static final long serialVersionUID = 0L; @java.lang.Override protected java.lang.Object writeReplace() throws java.io.ObjectStreamException { return super.writeReplace(); } public static com.example.test.Test.ClientKey parseFrom( com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { return newBuilder().mergeFrom(data).buildParsed(); } public static com.example.test.Test.ClientKey parseFrom( com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return newBuilder().mergeFrom(data, extensionRegistry) .buildParsed(); } public static com.example.test.Test.ClientKey parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { return newBuilder().mergeFrom(data).buildParsed(); } public static com.example.test.Test.ClientKey parseFrom( byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return newBuilder().mergeFrom(data, extensionRegistry) .buildParsed(); } public static com.example.test.Test.ClientKey parseFrom(java.io.InputStream input) throws java.io.IOException { return newBuilder().mergeFrom(input).buildParsed(); } public static com.example.test.Test.ClientKey parseFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return newBuilder().mergeFrom(input, extensionRegistry) .buildParsed(); } public static com.example.test.Test.ClientKey parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { Builder builder = newBuilder(); if (builder.mergeDelimitedFrom(input)) { return builder.buildParsed(); } else { return null; } } public static com.example.test.Test.ClientKey parseDelimitedFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { Builder builder = newBuilder(); if (builder.mergeDelimitedFrom(input, extensionRegistry)) { return builder.buildParsed(); } else { return null; } } public static com.example.test.Test.ClientKey parseFrom( com.google.protobuf.CodedInputStream input) throws java.io.IOException { return newBuilder().mergeFrom(input).buildParsed(); } public static com.example.test.Test.ClientKey parseFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return newBuilder().mergeFrom(input, extensionRegistry) .buildParsed(); } public static Builder newBuilder() { return Builder.create(); } public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder(com.example.test.Test.ClientKey prototype) { return newBuilder().mergeFrom(prototype); } public Builder toBuilder() { return newBuilder(this); } @java.lang.Override protected Builder newBuilderForType( com.google.protobuf.GeneratedMessage.BuilderParent parent) { Builder builder = new Builder(parent); return builder; } public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder> implements com.example.test.Test.ClientKeyOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return com.example.test.Test.internal_static_com_example_test_ClientKey_descriptor; } protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { return com.example.test.Test.internal_static_com_example_test_ClientKey_fieldAccessorTable; } // Construct using com.example.test.Test.ClientKey.newBuilder() private Builder() { maybeForceBuilderInitialization(); } private Builder(BuilderParent parent) { super(parent); maybeForceBuilderInitialization(); } private void maybeForceBuilderInitialization() { if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { } } private static Builder create() { return new Builder(); } public Builder clear() { super.clear(); id_ = 0; bitField0_ = (bitField0_ & ~0x00000001); name_ = ""; bitField0_ = (bitField0_ & ~0x00000002); flag_ = false; bitField0_ = (bitField0_ & ~0x00000004); return this; } public Builder clone() { return create().mergeFrom(buildPartial()); } public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { return com.example.test.Test.ClientKey.getDescriptor(); } public com.example.test.Test.ClientKey getDefaultInstanceForType() { return com.example.test.Test.ClientKey.getDefaultInstance(); } public com.example.test.Test.ClientKey build() { com.example.test.Test.ClientKey result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException(result); } return result; } private com.example.test.Test.ClientKey buildParsed() throws com.google.protobuf.InvalidProtocolBufferException { com.example.test.Test.ClientKey result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException( result).asInvalidProtocolBufferException(); } return result; } public com.example.test.Test.ClientKey buildPartial() { com.example.test.Test.ClientKey result = new com.example.test.Test.ClientKey(this); int from_bitField0_ = bitField0_; int to_bitField0_ = 0; if (((from_bitField0_ & 0x00000001) == 0x00000001)) { to_bitField0_ |= 0x00000001; } result.id_ = id_; if (((from_bitField0_ & 0x00000002) == 0x00000002)) { to_bitField0_ |= 0x00000002; } result.name_ = name_; if (((from_bitField0_ & 0x00000004) == 0x00000004)) { to_bitField0_ |= 0x00000004; } result.flag_ = flag_; result.bitField0_ = to_bitField0_; onBuilt(); return result; } public Builder mergeFrom(com.google.protobuf.Message other) { if (other instanceof com.example.test.Test.ClientKey) { return mergeFrom((com.example.test.Test.ClientKey)other); } else { super.mergeFrom(other); return this; } } public Builder mergeFrom(com.example.test.Test.ClientKey other) { if (other == com.example.test.Test.ClientKey.getDefaultInstance()) return this; if (other.hasId()) { setId(other.getId()); } if (other.hasName()) { setName(other.getName()); } if (other.hasFlag()) { setFlag(other.getFlag()); } this.mergeUnknownFields(other.getUnknownFields()); return this; } public final boolean isInitialized() { if (!hasId()) { return false; } if (!hasName()) { return false; } return true; } public Builder mergeFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder( this.getUnknownFields()); while (true) { int tag = input.readTag(); switch (tag) { case 0: this.setUnknownFields(unknownFields.build()); onChanged(); return this; default: { if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) { this.setUnknownFields(unknownFields.build()); onChanged(); return this; } break; } case 8: { bitField0_ |= 0x00000001; id_ = input.readInt32(); break; } case 18: { bitField0_ |= 0x00000002; name_ = input.readBytes(); break; } case 24: { bitField0_ |= 0x00000004; flag_ = input.readBool(); break; } } } } private int bitField0_; // required int32 id = 1; private int id_ ; public boolean hasId() { return ((bitField0_ & 0x00000001) == 0x00000001); } public int getId() { return id_; } public Builder setId(int value) { bitField0_ |= 0x00000001; id_ = value; onChanged(); return this; } public Builder clearId() { bitField0_ = (bitField0_ & ~0x00000001); id_ = 0; onChanged(); return this; } // required string name = 2; private java.lang.Object name_ = ""; public boolean hasName() { return ((bitField0_ & 0x00000002) == 0x00000002); } public String getName() { java.lang.Object ref = name_; if (!(ref instanceof String)) { String s = ((com.google.protobuf.ByteString) ref).toStringUtf8(); name_ = s; return s; } else { return (String) ref; } } public Builder setName(String value) { if (value == null) { throw new NullPointerException(); } bitField0_ |= 0x00000002; name_ = value; onChanged(); return this; } public Builder clearName() { bitField0_ = (bitField0_ & ~0x00000002); name_ = getDefaultInstance().getName(); onChanged(); return this; } void setName(com.google.protobuf.ByteString value) { bitField0_ |= 0x00000002; name_ = value; onChanged(); } // optional bool flag = 3 [default = false]; private boolean flag_ ; public boolean hasFlag() { return ((bitField0_ & 0x00000004) == 0x00000004); } public boolean getFlag() { return flag_; } public Builder setFlag(boolean value) { bitField0_ |= 0x00000004; flag_ = value; onChanged(); return this; } public Builder clearFlag() { bitField0_ = (bitField0_ & ~0x00000004); flag_ = false; onChanged(); return this; } // @@protoc_insertion_point(builder_scope:com.example.test.ClientKey) } static { defaultInstance = new ClientKey(true); defaultInstance.initFields(); } // @@protoc_insertion_point(class_scope:com.example.test.ClientKey) } private static com.google.protobuf.Descriptors.Descriptor internal_static_com_example_test_ClientKey_descriptor; private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_com_example_test_ClientKey_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { return descriptor; } private static com.google.protobuf.Descriptors.FileDescriptor descriptor; static { java.lang.String[] descriptorData = { "\n\ntest.proto\022\020com.example.test\":\n\tClient" + "Key\022\n\n\002id\030\001 \002(\005\022\014\n\004name\030\002 \002(\t\022\023\n\004flag\030\003 " + "\001(\010:\005false" }; com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { public com.google.protobuf.ExtensionRegistry assignDescriptors( com.google.protobuf.Descriptors.FileDescriptor root) { descriptor = root; internal_static_com_example_test_ClientKey_descriptor = getDescriptor().getMessageTypes().get(0); internal_static_com_example_test_ClientKey_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable( internal_static_com_example_test_ClientKey_descriptor, new java.lang.String[] { "Id", "Name", "Flag", }, com.example.test.Test.ClientKey.class, com.example.test.Test.ClientKey.Builder.class); return null; } }; com.google.protobuf.Descriptors.FileDescriptor .internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] { }, assigner); } // @@protoc_insertion_point(outer_class_scope) }
|
Вот такой кодик Цитата(Pawl @ 5.6.2014, 23:54 ) | А то вдруг там за баги наказывают! |
Наказывают , со временем привыкнешь, а потом даже нанешь получать удовольствие
--------------------
Disclaimer: this post contains explicit depictions of personal opinion. So, if it sounds sarcastic, don't take it seriously. If it sounds dangerous, do not try this at home or at all. And if it offends you, just don't read it.
|