c# - PubNub JSON serialization code works in example project but not in my project

Posted by pilau on Stack Overflow See other posts from Stack Overflow or by pilau
Published on 2012-09-15T09:17:09Z Indexed on 2012/09/15 9:37 UTC
Read the original article Hit count: 665

Filed under:
|
|
|
|

I am making a Winamp plugin with the single function of sending the details of the song being played over HTTP to a webpage.

It works like this: Winamp song event triggered -> check for new song -> publish to webpage using PubNub (C# API).

So far I got to the stage where everything works exactly as it is supposed to, except for the PubNub code which doesn't serialize the object I'm passing for publishing into JSON. All I keep getting in the PubNub console is a mere {} - an empty JSON object.

A little background on the project structure:

Project breakdown

I am using Sharpamp which is a custom library that enables making Winamp plugins with C#. I am also using the PubNub C# API. The gen_notifier_cs project is the C++ plugin wrapper created by Sharpamp. notifier_cs is where all my code resides. The two other projects are self explanatory I assume. I have referenced the PubNub API in notifier_cs, and also have referenced Sharpamp in both notifier_cs and PubNub API.

So, the objects that need to get serialized are of a class Song as defined in Sharpamp:

    public class Song
{
    public string Title { get; internal set; }
    public string Artist { get; internal set; }
    public string Album { get; internal set; }
    public string Year { get; internal set; }
    public bool HasMetadata { get; internal set; }
    public string Filename { get; internal set; }
}

So let's say if I have a song object with song data in it, I would go pubnub.publish("winamp_pipe", song); to publish it and PubNub will automatically serialize the data into JSON. But that's just not working in my solution.

To test why it wasn't serializing, I copied that class to the example code file in the PubNub API. Visual Studio changed the class to this (notice the public Song() method):

    public class Song
{
    public Song()
    {
        return;
    }

    public string Album { get; set; }
    public string Artist { get; set; }
    public string Filename { get; set; }
    public bool HasMetadata { get; set; }
    public string Title { get; set; }
    public string Year { get; set; }
}

On the same example file I initiated a default song object with some values:

        Song song = new Song();
        song.Album = "albumname";
        song.Artist = "artistname";
        song.HasMetadata = true;
        song.Title = "songtitle";
        song.Year = "2012";

And published it: pubnub.publish("winamp_pipe", song); and it worked! I got the JSON object in the PubNub channel!

{"Album":"albumname","Artist":"artistname","Filename":null,"HasMetadata":true,"Title":"songtitle","Year":"2012"}

So, I tried replacing the "new" Song class with the original one defined in Sharpamp. I tried adding another class definition in the notifier_cs project but that clashes with the one in Sharpamp which I have to rely on. I have been trying so many things as far as I could come up with. Needless to say none prevailed. Still, all I get is an empty JSON object.

I have been pulling out my hair for the last day. I know this post is super long but I appreciate your input nonetheless.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON