quez написав:FakiNyan написав:quez написав:Так чому не передавати поля джсоном, а на місці ініціалізувати клас значеннями цих полів, добутими з джсона?
так json же може людина навіть читати, ага? ну в плані відкрити як текстовий файлик і почитати все?
А чому ви замість джсона не можете взяти інший формат, який не можна так почитати?
▼Прихований текст
На єврейському форумі ти ставиш питання, потім тобі ставлять питання. Пора міняти аватарку.
взагалі то я підводив до того, шо отой protobuf являється отим самим іншим форматом, котрий не можна так почитати, і там тіпо все краще зтиснути і т.д.
але я от глянув на клас, котрий зробив мені компілятор protobuf'а, і от з такого файлику
message MyMessage{
required int32 id=1;
optional string tag=2;
repeated float array=3;
}
воно зробило мені такий
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: first.proto
public final class First {
private First() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface MyMessageOrBuilder extends
// @@protoc_insertion_point(interface_extends:MyMessage)
com.google.protobuf.MessageOrBuilder {
/**
* <code>required int32 id = 1;</code>
*/
boolean hasId();
/**
* <code>required int32 id = 1;</code>
*/
int getId();
/**
* <code>optional string tag = 2;</code>
*/
boolean hasTag();
/**
* <code>optional string tag = 2;</code>
*/
java.lang.String getTag();
/**
* <code>optional string tag = 2;</code>
*/
com.google.protobuf.ByteString
getTagBytes();
/**
* <code>repeated float array = 3;</code>
*/
java.util.List<java.lang.Float> getArrayList();
/**
* <code>repeated float array = 3;</code>
*/
int getArrayCount();
/**
* <code>repeated float array = 3;</code>
*/
float getArray(int index);
}
/**
* Protobuf type {@code MyMessage}
*/
public static final class MyMessage extends
com.google.protobuf.GeneratedMessage implements
// @@protoc_insertion_point(message_implements:MyMessage)
MyMessageOrBuilder {
// Use MyMessage.newBuilder() to construct.
private MyMessage(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private MyMessage(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final MyMessage defaultInstance;
public static MyMessage getDefaultInstance() {
return defaultInstance;
}
public MyMessage getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private MyMessage(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 8: {
bitField0_ |= 0x00000001;
id_ = input.readInt32();
break;
}
case 18: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000002;
tag_ = bs;
break;
}
case 29: {
if (!((mutable_bitField0_ & 0x00000004) == 0x00000004)) {
array_ = new java.util.ArrayList<java.lang.Float>();
mutable_bitField0_ |= 0x00000004;
}
array_.add(input.readFloat());
break;
}
case 26: {
int length = input.readRawVarint32();
int limit = input.pushLimit(length);
if (!((mutable_bitField0_ & 0x00000004) == 0x00000004) && input.getBytesUntilLimit() > 0) {
array_ = new java.util.ArrayList<java.lang.Float>();
mutable_bitField0_ |= 0x00000004;
}
while (input.getBytesUntilLimit() > 0) {
array_.add(input.readFloat());
}
input.popLimit(limit);
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
if (((mutable_bitField0_ & 0x00000004) == 0x00000004)) {
array_ = java.util.Collections.unmodifiableList(array_);
}
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return First.internal_static_MyMessage_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return First.internal_static_MyMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
First.MyMessage.class, First.MyMessage.Builder.class);
}
public static com.google.protobuf.Parser<MyMessage> PARSER =
new com.google.protobuf.AbstractParser<MyMessage>() {
public MyMessage parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new MyMessage(input, extensionRegistry);
}
};
@java.lang.Override
public com.google.protobuf.Parser<MyMessage> getParserForType() {
return PARSER;
}
private int bitField0_;
public static final int ID_FIELD_NUMBER = 1;
private int id_;
/**
* <code>required int32 id = 1;</code>
*/
public boolean hasId() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 id = 1;</code>
*/
public int getId() {
return id_;
}
public static final int TAG_FIELD_NUMBER = 2;
private java.lang.Object tag_;
/**
* <code>optional string tag = 2;</code>
*/
public boolean hasTag() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>optional string tag = 2;</code>
*/
public java.lang.String getTag() {
java.lang.Object ref = tag_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
tag_ = s;
}
return s;
}
}
/**
* <code>optional string tag = 2;</code>
*/
public com.google.protobuf.ByteString
getTagBytes() {
java.lang.Object ref = tag_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
tag_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int ARRAY_FIELD_NUMBER = 3;
private java.util.List<java.lang.Float> array_;
/**
* <code>repeated float array = 3;</code>
*/
public java.util.List<java.lang.Float>
getArrayList() {
return array_;
}
/**
* <code>repeated float array = 3;</code>
*/
public int getArrayCount() {
return array_.size();
}
/**
* <code>repeated float array = 3;</code>
*/
public float getArray(int index) {
return array_.get(index);
}
private void initFields() {
id_ = 0;
tag_ = "";
array_ = java.util.Collections.emptyList();
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
if (!hasId()) {
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, getTagBytes());
}
for (int i = 0; i < array_.size(); i++) {
output.writeFloat(3, array_.get(i));
}
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, getTagBytes());
}
{
int dataSize = 0;
dataSize = 4 * getArrayList().size();
size += dataSize;
size += 1 * getArrayList().size();
}
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 First.MyMessage parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static First.MyMessage parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static First.MyMessage parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static First.MyMessage parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static First.MyMessage parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static First.MyMessage parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static First.MyMessage parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static First.MyMessage parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static First.MyMessage parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static First.MyMessage parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(First.MyMessage 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;
}
/**
* Protobuf type {@code MyMessage}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:MyMessage)
First.MyMessageOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return First.internal_static_MyMessage_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return First.internal_static_MyMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
First.MyMessage.class, First.MyMessage.Builder.class);
}
// Construct using First.MyMessage.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.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);
tag_ = "";
bitField0_ = (bitField0_ & ~0x00000002);
array_ = java.util.Collections.emptyList();
bitField0_ = (bitField0_ & ~0x00000004);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return First.internal_static_MyMessage_descriptor;
}
public First.MyMessage getDefaultInstanceForType() {
return First.MyMessage.getDefaultInstance();
}
public First.MyMessage build() {
First.MyMessage result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public First.MyMessage buildPartial() {
First.MyMessage result = new First.MyMessage(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.tag_ = tag_;
if (((bitField0_ & 0x00000004) == 0x00000004)) {
array_ = java.util.Collections.unmodifiableList(array_);
bitField0_ = (bitField0_ & ~0x00000004);
}
result.array_ = array_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof First.MyMessage) {
return mergeFrom((First.MyMessage)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(First.MyMessage other) {
if (other == First.MyMessage.getDefaultInstance()) return this;
if (other.hasId()) {
setId(other.getId());
}
if (other.hasTag()) {
bitField0_ |= 0x00000002;
tag_ = other.tag_;
onChanged();
}
if (!other.array_.isEmpty()) {
if (array_.isEmpty()) {
array_ = other.array_;
bitField0_ = (bitField0_ & ~0x00000004);
} else {
ensureArrayIsMutable();
array_.addAll(other.array_);
}
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
if (!hasId()) {
return false;
}
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
First.MyMessage parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (First.MyMessage) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
private int id_ ;
/**
* <code>required int32 id = 1;</code>
*/
public boolean hasId() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 id = 1;</code>
*/
public int getId() {
return id_;
}
/**
* <code>required int32 id = 1;</code>
*/
public Builder setId(int value) {
bitField0_ |= 0x00000001;
id_ = value;
onChanged();
return this;
}
/**
* <code>required int32 id = 1;</code>
*/
public Builder clearId() {
bitField0_ = (bitField0_ & ~0x00000001);
id_ = 0;
onChanged();
return this;
}
private java.lang.Object tag_ = "";
/**
* <code>optional string tag = 2;</code>
*/
public boolean hasTag() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>optional string tag = 2;</code>
*/
public java.lang.String getTag() {
java.lang.Object ref = tag_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
tag_ = s;
}
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>optional string tag = 2;</code>
*/
public com.google.protobuf.ByteString
getTagBytes() {
java.lang.Object ref = tag_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
tag_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string tag = 2;</code>
*/
public Builder setTag(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000002;
tag_ = value;
onChanged();
return this;
}
/**
* <code>optional string tag = 2;</code>
*/
public Builder clearTag() {
bitField0_ = (bitField0_ & ~0x00000002);
tag_ = getDefaultInstance().getTag();
onChanged();
return this;
}
/**
* <code>optional string tag = 2;</code>
*/
public Builder setTagBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000002;
tag_ = value;
onChanged();
return this;
}
private java.util.List<java.lang.Float> array_ = java.util.Collections.emptyList();
private void ensureArrayIsMutable() {
if (!((bitField0_ & 0x00000004) == 0x00000004)) {
array_ = new java.util.ArrayList<java.lang.Float>(array_);
bitField0_ |= 0x00000004;
}
}
/**
* <code>repeated float array = 3;</code>
*/
public java.util.List<java.lang.Float>
getArrayList() {
return java.util.Collections.unmodifiableList(array_);
}
/**
* <code>repeated float array = 3;</code>
*/
public int getArrayCount() {
return array_.size();
}
/**
* <code>repeated float array = 3;</code>
*/
public float getArray(int index) {
return array_.get(index);
}
/**
* <code>repeated float array = 3;</code>
*/
public Builder setArray(
int index, float value) {
ensureArrayIsMutable();
array_.set(index, value);
onChanged();
return this;
}
/**
* <code>repeated float array = 3;</code>
*/
public Builder addArray(float value) {
ensureArrayIsMutable();
array_.add(value);
onChanged();
return this;
}
/**
* <code>repeated float array = 3;</code>
*/
public Builder addAllArray(
java.lang.Iterable<? extends java.lang.Float> values) {
ensureArrayIsMutable();
com.google.protobuf.AbstractMessageLite.Builder.addAll(
values, array_);
onChanged();
return this;
}
/**
* <code>repeated float array = 3;</code>
*/
public Builder clearArray() {
array_ = java.util.Collections.emptyList();
bitField0_ = (bitField0_ & ~0x00000004);
onChanged();
return this;
}
// @@protoc_insertion_point(builder_scope:MyMessage)
}
static {
defaultInstance = new MyMessage(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:MyMessage)
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_MyMessage_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_MyMessage_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\013first.proto\"3\n\tMyMessage\022\n\n\002id\030\001 \002(\005\022\013" +
"\n\003tag\030\002 \001(\t\022\r\n\005array\030\003 \003(\002"
};
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;
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
internal_static_MyMessage_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_MyMessage_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_MyMessage_descriptor,
new java.lang.String[] { "Id", "Tag", "Array", });
}
// @@protoc_insertion_point(outer_class_scope)
}
майбуть, краще самому ту серіалізацію робити, а то тут чірт ногу зломить