5

I have a list of object as IEnumerable<IHit<Header>>, but .NET Core 3.0 is not emitting all information

this is when I return a single object IHit of the list obj.ElementAt(0)

{"explanation":null,"fields":null,"highlight":{},"id":"123","index":"ky","innerHits":{},"matchedQueries":[],"nested":null, "primaryTerm":null, "routing":null, "score":10.98915, "sequenceNumber":null, "sorts":[],"source":{"timeStamp":"2019-05-16T06:16:07Z", "result":"PASS","testTimeStart":"20190516141559","testTimeEnd":"20190516141607","barcode":"XXRX8762"},"type":"_doc","version":0}

but when I return the list itself:

[{"explanation":null,"fields":null,"highlight":{},"innerHits":{},"matchedQueries":[],"nested":null,"score":10.98915,"sorts":[]}]

also when I try explicitly to call JsonSerializer

string s = JsonSerializer.Serialize(result, typeof(IEnumerable<IHit<Header>>), obj);

To Wrap Up:

  1. WebApi is working correctly and returning the object serialized correctly if a signle object:

    public IHit<Header> Get(string id)

but when a list, then it is not.

public IEnumerable<IHit<Header>> GetAll()

I tried IEnumerable, IList, List and all the same result!

  1. JsonSerializer is working neither with a single object nor with a list

5 Answers 5

20

I still don't know the weird behavior I faced due to emitting a single object while not the case of list.

but here how solved the problem based on @poke's answer and @NeilMacMullen's comment

  1. add package Microsoft.AspNetCore.Mvc.NewtonsoftJson
  2. in Startup.cs, add

    services.AddControllers().AddNewtonsoftJson();

3
  • 4
    I personally find it ridiculous that Microsoft rolls out its half-baked serializer as a default solution for .NET Core 3.0 projects. I mean, It indeed is twice as fast as Newtonsoft, but it comes with the price of having less than half the functionality mutilated.
    – mg30rg
    Mar 2, 2020 at 13:19
  • 2
    As of today, Jun, 2022, I am using dotnet 6, this is not working.. very frustrated
    – B.W
    Jun 23, 2022 at 6:13
  • @B.W, I have used the code on .NET Core 3.0, don't know about .NET 6 Jun 24, 2022 at 2:55
6

Add the attribute [JsonInclude] with .Net 6 for it to serialize lists

e.g.

   [JsonInclude]
   public List<StaffEarningDetail> EarningDetails;
2

If your are using Newtonsoft.Json then do as follows:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        );

    ...
}

If you are not using Newtonsoft.Json then do as follows:

public void ConfigureServices(IServiceCollection services)
{
    ...

   services.AddMvc().AddJsonOptions(option => option.JsonSerializerOptions.MaxDepth = 2);

    ...
}

For more details : Related data and serializatione

0

JsonSerializer likely does not know how to serialize IHit<T>. I would propose one of:

Getting the JSON string response from Elasticsearch, using either

  1. the low level client, exposed on the high level client as the .LowLevel property. You can serialize a high level request type using PostData.Serializable(request)
  2. using OnRequestCompleted() and setting DisableDirectStreaming() on the request or globally on ConnectionSettings

or

Mapping the response from Elasticsearch to another type that JsonSerializer will serialize.

2
  • thanks I will try this regarding JsonSerializer, but why WebApi is able to return a single object IHit<Header> serialized correctly, but when returning the whole list IEnumerable<IHit<Header>> then it is not! Sep 27, 2019 at 3:16
  • I don't know off the top of my head, I'm afraid
    – Russ Cam
    Sep 27, 2019 at 3:19
0

In my case I've mistakenly written internal set function at ViewModel class properties as follows;

public int CampaignServiceFee { get; internal set; }

just removed the internal keyword so that outside of class, JsonSerializer, can set values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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