How can I map java.lang.Object in .proto file. I'd like to have smth like:
message User {
string name = 1;
Object field = 2;
}
Disregarding the strangeness of your requirement for a moment: serialize your Java object to byte[] (formally implementing Serializable, or any way you want) and use protobuf value type bytes.
But this is really not the way to use protobuf. It's meant to transfer data, not objects. What of your object you would like to transfer over the wire? You cannot serialize methods and nontrivial object dependencies anyway. Only value types and simple structures (arrays, maps). So declare a proto message which reflects the data structure of your object, and serialize to that instead of byte[].
Related
I have a proxy service that translate protobuf into another struct. I
can just write some manual code to do that, but that is inefficient and boilerplate. I can also transform the protobuf data to JSON, and deserlize the JSON data into the destination struct, but the speed is slow and it is CPU heavy.
The Unmarshaler interface is now deprecated, and Message interface have internal types which I cannot implement in my project.
Is there a way I can do this now?
Psuedo code: basically, if Go's reflection supports setting and getting of struct / class fields by some sort of field identifier, then you can do this. Something like this in C# works, so long as the field types in the two classes are the same (because in C#, I'm doing object = object, which ends up being OK if they're the same actual type).
SourceStructType sourceStruct;
DestStructType destStruct;
foreach (Field sourceField in sourceStruct.GetType().GetFields())
{
Field destField = destStruct.GetType().FindFieldByName(sourceField.name);
destStruct.SetFieldValue(destField) = sourceStruct.GetFieldValue(sourceField);
}
If the structs are more complex - i.e. they have structs within them, then you'll have to recurse down into them. It can get fiddly, but once written you'll never have to write it ever again!
In google protobuf v3 there is the Any type that allows you to serialize a name/url identifying the message (https://developers.google.com/protocol-buffers/docs/proto3#any, https://github.com/google/protobuf/blob/master/src/google/protobuf/any.proto).
Is it possible to describe this construct and have protobuf-net serialize/deserialize it? I need it to follow protobuf v3 standard, so DynamicType/bcl.NetObjectProxy is close, but not binary compatible.
If not, what is the best way to serialize objects with Any constructs using protobuf-net? I really don't want to switch to protobuf-csharp version.
Ultimately, Any can be treated as just:
[ProtoContract]
public sealed class Any {
[ProtoMember(1)] public string type_url {get;set;}
[ProtoMember(2)] public byte[] value {get;set;}
}
From there, you could do a switch (or a dictionary lookup) on the url, load the value into a MemoryStream, and call back into protobuf-net. For serialization, the type url could perhaps be stored in an attribute against the type, but that is trickier fog deserialization. Maybe a string-to-Type dictionary and the non-generic API.
I want to serialize the following DTO using Protocol Buffer . Can somebody let me know what would be the .proto file entry for java Object type.
public class Person {
private Object role;
}
A bytes, that is a ByteString in Java, and a ByteString can be easily converted to a byte[] or an InputStream which you can e.g. warp in an ObjectInputStream and therefore read your serialized Object from it.
It's not very hard, but also not very protobuf-ish and/or a wise thing to do. I suggest saving some logical representation of your role object into the protobuf instead. I'm sure there is a solid option for you somewhere - be it a String, or an enum or something, you should save some the logical state of your role instead of the actual serialized object, and then try to construct the object upon retrieval.
I can't seem to find a concrete example of how to write a custom converter for Restlet. I'm having an issue with an object containing an ArrayList of a base type. (ie List )
I've been able to painstakingly do the conversion by reading in the object as JSON, converting it to a JSONObject and processing each field individually to create the representation I need on the deserialization. This seems very odd that I'd have to do this.
any help would be appreciated.
How do we pass objects of some custom class as a parameter to mapper in mapReduce programs??
JobConf has 'set' methods for boolean, string, int and long. What if I want to pass a Document object as a parameter to my mapper? Can any one help me out?
I have given a tip to someone who wanted a whole map to pass to a mapper.
Hadoop: How to save Map object in configuration
The idea is the same, you have to serialize your object into a string and put it into the configuration. JSON works very well, because the configuration is serialized as XML, thus having no problem while deserializing.
If your object implements Writable, you can serialize it to a Byte array, base64 encode the byte array and then save off the resultant string to the configuration. To decode do the opposite.
Of course, i wouldn't recommend this if your object has a very large footprint - in this case you're better off serializing it to a file in HDFS and using the distributed cache.