0

I can't seem to find anything in the OpenRasta docs or tutorials that shows how to use arbitrary JSON objects (i.e. objects not predefined using C# classes) for both receiving from and responding back to the client.

One way to do it would be to use JsonValue and write a custom codec that would just use the (de)serialization features provided by JsonValue. That should be pretty straightforward and less than 50 lines of code, but I wondered if there isn't anything built into OpenRasta?

(One downside of JsonValue is that MS has not yet released it, so you can't yet deploy it to customers (see 1. "Additional Use Rights"). But in cases where that matters, any other Json library, like Json.NET can be used.)

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156

2 Answers2

0

I just implemented a JSON codec using JsonFx. It goes like this:

using System.IO;
using System.Text;
using JsonFx.Json;


namespace Example
{
  [global::OpenRasta.Codecs.MediaType("application/json")]
  public class JsonFXCodec : global::OpenRasta.Codecs.IMediaTypeWriter, global::OpenRasta.Codecs.IMediaTypeReader
  {
    public void WriteTo(object entity, global::OpenRasta.Web.IHttpEntity response, string[] codecParameters)
    {
      JsonWriter json = new JsonWriter();
      using (TextWriter w = new StreamWriter(response.Stream, Encoding.UTF8))
      {
        json.Write(entity, w);
      }
    }


    public object ReadFrom(global::OpenRasta.Web.IHttpEntity request, global::OpenRasta.TypeSystem.IType destinationType, string destinationName)
    {
      JsonReader json = new JsonReader();
      using (TextReader r = new StreamReader(request.Stream, Encoding.UTF8))
      {
        return json.Read(r, destinationType.StaticType);
      }
    }


    public object Configuration { get; set; }
  }
}

If it is registered for "object" then it seems to work for any class:

ResourceSpace.Has.ResourcesOfType<object>()
                 .WithoutUri
                 .TranscodedBy<JsonFXCodec>();
Jørn Wildt
  • 4,274
  • 1
  • 21
  • 31
0

I have written, like most people, a very simple codec that supports dynamics as inputs and outputs to handlers using json.net. You can also register that codec with an anonymous type and it works brilliantly. You end up with this:

public object Post(dynamic myCustomer) {
  return new { response = myCustomer.Id };
}
SerialSeb
  • 6,701
  • 24
  • 28
  • Want to share your implementation so everyone can use it? It seems like a common usecase, especially when working with a REST API that allows for dynamic JSON objects that you can't specify class files for. You are the author of OpenRasta, right? I guess you would find some place to put it online... :) P.S. Your implied answer was: "There's nothing built into OpenRasta", is that correct? – Evgeniy Berezovsky Sep 16 '11 at 02:16
  • 1
    I can push the package on openwrap this weekend sure, it's rather simple. May be worth a blog post too. One of the tricks you can use in OpenRasta is that registrations for resources without URIs respect inheritance, so if you register for then anything that inherits from object (which is everything) will be able to be sent to the codec. – SerialSeb Sep 16 '11 at 19:14
  • That would be awesome. In you case you get around to it, please also add that to your answer. – Evgeniy Berezovsky Sep 20 '11 at 04:52