Issue with Json serializer


#1

Hi.

I have wasted a whole day to figureout why my code fails. I am getting {“The method or operation is not implemented.”}. I am able to get serialized object with other IEnumeration object.

Couchbase community edition 2.2
.NET 4.5.1

IEnumerable Sdata = SensorRepository.DoorEventsRepository.GetAllByAlarms();

        HttpResponseMessage Response = new HttpResponseMessage();

        if (Sdata != null)
        {
            var JsonData = JsonConvert.SerializeObject(Sdata);// FAILS with error

            StringContent SData = new StringContent(JsonData);
            SData.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            Response.Content = SData;
        }
        else
        {
            Response.Content = new StringContent("Nothing");
        }

Here is the complete error log:

System.NotImplementedException was unhandled by user code
HResult=-2147467263
Message=The method or operation is not implemented.
Source=Couchbase
StackTrace:
at Couchbase.CouchbaseViewBase1.System.Collections.IEnumerable.GetEnumerator() at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) at LynxCouchBase.Controllers.RemoteLoginController.Sensors(FormDataCollection Value) in d:\Software_Projects\Project_files\WirelessProtocols\DataBase\LynxCouchBase\LynxCouchBase\Controllers\RemoteLoginController.cs:line 129 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken)
InnerException:

Any help is much appreciated. I have given up on this.

-PP


#2

hey @pappu_pandit, it seems you are using branch 1.x of the .Net SDK, which I’m not terribly familiar with, but I’ll try to help…

I guess what you do in GetAllByAlarms() is query a view, and return the result as is. This result you then try to serialize to JSON to send back to the client, am I right?

If that’s the case, the enumerable result produced by querying the view has not been made to be serialized. It may be a bug / overlook, or it may just be that you’re not supposed to expose that to the outside world.

You should probably instead iterate over this result to extract meaningful data (it should be from IViewRow objects), and return your own collection. Then serialize and serve this collection to the client.

@jmorris do you want to chime in?


#3

Hi simonbasle,

You are absolutely right about what I am trying to do. Thanks for your suggestion. I am going to try that out. Are you familiar with the issue fixed with new version SDK?

Thanks again.


#4

@pappu_pandit new version SDK is a complete rewrite. Looking at the source, IViewRow in there seems to be perfectly serializable to JSON.


#5

Hi simonbasle,

It is the case that IViewRow is serializable; however I wanted to serialize IEnumerable object returned by GetAllByAlarms() and that’s where I got lost. Thanks for your tip. I simply converted the returned object to List and everything is working fine.

Here is the code snippet that works now:

        var Sdata = SensorRepository.GetAllByRoles("NYTRIC", "DoorSensor"); // Replaced required view instead of simplified view mentioned previously

        List<Sensors> ListData = Sdata.ToList();

        HttpResponseMessage Response = new HttpResponseMessage();

        if (Sdata != null)
        {
            var JsonData = JsonConvert.SerializeObject(ListData);
            StringContent SData = new StringContent(JsonData);
            SData.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            Response.Content = SData;
        }
        else
        {
            Response.Content = new StringContent("Nothing");
        }


        return Response;

Thanks a Lot.

PP


#6

glad I could help :smile: