Comments (7)
Hi mchouaha.
As ypu can see issue #21, msgpack-cli does not support serialize non-public members, but it supports composite type. So, try following:
- Make sure that fields of your types are public or they have public properties which correspond to the fields. Note that they will be private when you do not qualify members with accessibiliy like 'public'.
- Create a
serializer instance viaMessagePackSerializer.Create<KNNQueryResponse>()
, and call itsPack
orUnpack
method.
from msgpack-cli.
HI yfakariya,
Thank you very much for your quick reply. I am still having the same problem unfortunately ;( .
Here is my method for desterilizing the KNNQueryResponse class:
public void deserialize(Byte[] bytes)
{
var unpackKNN = MessagePackSerializer.Create < KNNQueryResponse > ();
KNNQueryResponse tmp;
using (var byteStream = new MemoryStream(bytes))
{
tmp = unpackKNN.Unpack(byteStream);
System.Diagnostics.Debug.WriteLine("results list size: " + tmp.size);
System.Diagnostics.Debug.WriteLine("results list size: " + tmp.results.count);
}
} ;
The output prints out:
results list size = 0;
results list size = 0;
I checked if the problem didn't come from the stream of bytes I passed as a parameter in the function deserialize(Byte[] bytes), so I just checked the length of bytes and it is 78247. So I don't think it comes from the stream of bytes.
Another, it is very strange but when I try to to create an instance of MessagePackSerializer.Create < int > (), and then call the unpack method, I get back a value 10.
The output prints out:
results list size = 10;
Any idea?
from msgpack-cli.
I tried locally as following, but it did not reproduce your problem. So, would you like to attach sample code to reproduce your problem, or would you show me first few bytes of bytes
?
from msgpack-cli.
You will find above the class Trending that create connection between client and server.
Trending create an instance of KNNQueryResponse to deserialize the stream of byte received from the server
GetRequestStreamCallback() : create http request and send json object to the server
GetResponseCallback() : receive msgPack from server
ReadFully() : convert srteam to an array of byte
public partial class TrendingPage : PhoneApplicationPage
{
public TrendingPage()
{
InitializeComponent();
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp( url ) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "POST";
request.Accept = "application/octet-stream";
// start the asynchronous operation
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
public static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
JObject json = new JObject();
json.Add("country", "IE");
json.Add("categoryId", "198");
json.Add("productID", "null");
json.Add("uid", "");
json.Add("deviceModel", "");
json.Add("deviceOS", "");
string postData = json.ToString();
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
public static void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
StreamReader streamRead = new StreamReader(response.GetResponseStream());
byte[] bytes = ReadFully(response.GetResponseStream());
//Print size of stream bytes
System.Diagnostics.Debug.WriteLine("byte stream length = " + bytes.Length);
//Print stream bytes
var sb = new StringBuilder("new byte[] { ");
foreach(var b in bytes)
{
sb.Append(b + ", ");
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
KNNQueryResponse kqr;
kqr = new KNNQueryResponse();
kqr.deserialize(bytes);
// Release the HttpWebResponse
response.Close();
}
catch (WebException e)
{
System.Diagnostics.Debug.WriteLine("exception: " + e.StackTrace);
System.Diagnostics.Debug.WriteLine("exception: " + e.Message);
}
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
}
Furthermore, the first few bytes printed out from the array of bytes look like this:
new byte[] { 10,
new byte[] { 10, 218,
new byte[] { 10, 218, 31,
new byte[] { 10, 218, 31, 7,
new byte[] { 10, 218, 31, 7, 218,
new byte[] { 10, 218, 31, 7, 218, 0,
new byte[] { 10, 218, 31, 7, 218, 0, 203,
new byte[] { 10, 218, 31, 7, 218, 0, 203, 104,
new byte[] { 10, 218, 31, 7, 218, 0, 203, 104, 116,
new byte[] { 10, 218, 31, 7, 218, 0, 203, 104, 116, 116,
....
.........
............
Does this help you to understand better my problem?
from msgpack-cli.
Thank you detailed response!
It looks that server responds invalid stream as msgpack. For example, the 1st byte 10' means 'integer 10', does not mean begging of serialized object. And from 2nd to 6th bytes mean that it is a dictionary which has 7,943 entries, and its first entry's key is also a dictionary which has 203 entries and its first entry's key is an integer 104... I guess that it is not common that a key of dictionary is another dictionary.
Does this help you?
from msgpack-cli.
Very good deduction! In fact, I just noticed that now, the server side doesn't pack the class KNNQueryResponse itself, but individually each object of that class, same for the class ImageResult, each conctructor is packed individually. Apparently it has been done this way, cause the guy who wrote the server side could no pack just the class only.
Thank you very much yfakariya.
Problem solved:)
from msgpack-cli.
Close because it was solved.
from msgpack-cli.
Related Issues (20)
- Unpacker throws exception trying to read map length after IsMapHeader returns true HOT 1
- Buffering causes async unpacking to fail HOT 2
- Add a non-generic override to SerializerRepository
- EnumMemberAttribute does not work. (expectedly)
- Type Object in MessagePack HOT 1
- GetMemberName() causes NullReferenceException when using MessagePackSerializer.Get<T>() in Xamarin iOS
- [Unity][il2cpp]'class' is not registered in this resolver,resolver.StandardResolver HOT 2
- How to serialize and deserialize a List member in a class HOT 2
- Problem deserializing byte array - InvalidOperationException caused by Decoders.InvalidArrayHeader.Read call HOT 4
- about LZ4 HOT 1
- Unable to cast object of type on netcore HOT 1
- Custom Serialisation docs appear to be out of date. HOT 1
- PackAsync & UnpackAsync not working when passing data continuously using for loop c# HOT 1
- The unpacked value is not expected type HOT 2
- Authenticode Signing? HOT 2
- DateTime serialization and deserialization timezone HOT 3
- Enum Deserialization Not working? HOT 1
- Serializer.Unpack Continuosly Stream of Data => High Memory Usage HOT 3
- Serialization / deserialization speed optimizations HOT 2
- Serialization field order by declaration order HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack-cli.