Git Product home page Git Product logo

Comments (7)

yfakariya avatar yfakariya commented on August 17, 2024

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:

  1. 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'.
  2. Create a
    serializer instance via MessagePackSerializer.Create<KNNQueryResponse>(), and call its Pack or Unpack method.

from msgpack-cli.

mchouaha avatar mchouaha commented on August 17, 2024

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.

yfakariya avatar yfakariya commented on August 17, 2024

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.

mchouaha avatar mchouaha commented on August 17, 2024

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.

yfakariya avatar yfakariya commented on August 17, 2024

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.

mchouaha avatar mchouaha commented on August 17, 2024

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.

yfakariya avatar yfakariya commented on August 17, 2024

Close because it was solved.

from msgpack-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.