Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am building a wp7 application.I want to parse JSON string coming from a continuous tcp stream.I want a json parser which can parse from a stream.Is there any SAX JSON parser to parse from a stream.I am looking for a tcp stream parser.that is continuous stream is coming from my server.And i want to parse the stream and generate JSON objects on the fly.

share|improve this question

1 Answer

You can use JSON.NET library, you can get it here: http://json.codeplex.com/

I state from codeplex these features of JSON.NET

  • Flexible JSON serializer for converting between .NET objects and JSON
  • LINQ to JSON for manually reading and writing JSON
  • High performance, faster than .NET's built-in JSON serializers
  • Write indented, easy to read JSON
  • Convert JSON to and from XML
  • Supports .NET 2, .NET 3.5, .NET 4, Silverlight, Windows Phone and Windows 8.

and here is a little example:

public class MyClass
  {
    public string MyProperty;
    public string[] MyArray;        
  }
string json = JsonConvert.SerializeObject(new MyClass() { MyProperty = "Test",  MyArray = new string[] { "Test1", "Test2" } });
    //{"MyProperty":"Test","MyArray":["Test1","Test2"]}"

EDIT

Here is a little example:

      System.Net.WebClient client = new System.Net.WebClient();
      using (var stream = client.OpenRead(url))
      {      
        byte a = 0;
        var list = new List<byte>();
        do{
          a = (byte)stream.ReadByte();
          list.Add(a);
          if(/*Test if valid JSON string*/)
          {
            var str = System.Text.Encoding.UTF8.GetString(list.ToArray());
            JsonConvert.DeserializeObject<MyClass>(str);
          }
        }while(a != -1)
      }

you can use TcpClient to connect. Check this question Subscribing over TCP, deserializing each recieved JSON line

share|improve this answer
I am looking for a tcp stream parser.that is continuous stream is coming from my server.And i want to parse the stream and generated JSON objects on the fly. – tukir 20 hours ago
Can't you receive the stream part by part, save it to a string and then deserialize it? – Houssem 20 hours ago
note that you need all the JSON string to be able to deserialize it, so the only solution is to get the string from the web and then deserialize it – Houssem 20 hours ago
I am receiving a continuous stream containing several JSON strings.I need to separate them to get the real JSON objects. – tukir 20 hours ago
Yeah, thus is exactly my point, you have to seperate by checking for example for special character or sequence, and each time you find the sequence/character you deserialize the string – Houssem 19 hours ago
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.