title: Simple JSON.NET serialization/deserialization example
body: <p>I've been searching the internet for literally hours trying to find a very simple example of serialization and deserialization with a JSON call in C#. After careful browsing and piecing together, I'd like to call a JSON (POST &amp; GET) function in my webservice (see below).</p>
  
  <p>Here's what I was able to piece together</p>
  
  <p>This would be my service contract (IExecWebservice.svc)</p>
  
  <pre><code>using System.ServiceModel;
  using System.ServiceModel.Web;
  
  namespace _27963199
  {
      [ServiceContract]
      public interface IExecFunction
      {
          [WebGet(UriTemplate = "/{function}/{args}")]
          double CalcThis(string function, string args);
      }
  }
  </code></pre>
  
  <p>In my main code I parse out the users request URI (IExecFunction.cs)</p>
  
  <pre><code>//user will send variables in REST URI http://myCalcServer/CalcThis/MethodA/10,20
  using FunctionLibrary;
  using System;
  using System.Reflection;
  
  namespace _27963199
  {
      public class ExecFunctionService : IExecFunction
      {
          public double CalcThis(string function, string args)
          {
               Type t = typeof(Functions);
               MethodInfo[] libraryFunctions = t.GetMethods(BindingFlags.Static | BindingFlags.Public);
               string[] arguments = args.Split(',');
  
               //Missing piece of code where I split the URI for the JSON function that will POST the data object to be be calculated by the DROOLS/Rules Engine and the results passed back to the users web browser
  ...
           }
       }
  }
  </code></pre>
  
  <p>Now in my separate function class I'd have something like this (Function.cs)</p>
  
  <pre><code>using System;
  using newton.json;
  using System.Net.Http;
  
  namespace FunctionLibrary
  {
      public static class Functions
      {
          public static double DoMathA(string url, string arg1, string arg2)
          {
              double d1;
              double d2;
  
              if (!double.TryParse(arg1, out d1) || !double.TryParse(arg2, out d2))
              {
                  throw new ArgumentException("Arguments to function 'DoMathA' must be numeric.");
              }
  
              //Data Object Format "{'myData':{'Id':'5','var1':'10','var2':'90'}}"
              myCalcObject = "{'myData':{'Id':'5', &amp; arg1 &amp; :'10', &amp; arg2 &amp; :'90'}}"
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
              request.Method = "POST"; 
              request.ContentType = "application/json; charset=utf-8"; 
              DataContractJsonSerializer ser = new DataContractJsonSerializer(data.GetType()); 
              MemoryStream ms = new MemoryStream(); 
              ser.WriteObject (myCalcObject)
              String json = Encoding.UTF8.GetString(ms.ToArray()); 
              StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
              writer.Write(json); 
              writer.Close();
         }
  }
  ...
  
  //Missing piece of code where I want to return the results of the JSON PUT and GET to the calling webservice
  //JSON output string looks like this {"YourResults":{"condition":"YourHairIsOnFire","alertlevel":100,"id":0}}
  //return or parse (json) to XLMS on the users browser
  
   }
  </code></pre>
  
  <p>I need help filling in the blanks so that the request URI is parsed properly to be passed into the JSON function and the reply JSON string be translated back as an xlms on the users browser. Any thoughts?</p>
  
  <p>Update: I tried getting just the JSON section to work as a standalone C# class but I get that "Expected class..." error when I compile it.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web;
  using System.Net.Http;
  using System.Net;
  using Newtonsoft.Json;
  
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http://172.16.20.26:8080/myDrools/result); 
  request.Method = "POST"; 
  request.ContentType = "application/json; charset=utf-8"; 
  DataContractJsonSerializer ser = new DataContractJsonSerializer(data.GetType()); 
  MemoryStream ms = new MemoryStream(); 
  ser.WriteObject ("{'myData':{'Id':'5','var1':'4.5','var2':'8.7'}}")
  String json = Encoding.UTF8.GetString(ms.ToArray()); 
  StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
  writer.Write(json); 
  writer.Close();
  </code></pre>
  
  <p>What Am I doing wrong here? How can I get an XMLS output from this?</p>