Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialize enum wrapper
    primarykey
    data
    text
    <p>EDIT: Made changes to code for it to function correctly server side. Still getting the error:</p> <p>"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'BareCupboard.Models.RecipeIngredient[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Message', line 1, position 11."</p> <p>I am successfully deseralizing my server-side code for consumption by the client for normal entity framework models. I have however created a wrapper for my enum to enable server-side interactions to modify the data in the format of drop down lists etc.</p> <p>Upon desearlizing the enum gets stuck in a self referencing loop, it appears. </p> <p>What is the correct method to desearlize objects that are created in such a way.</p> <pre><code>public enum ingredientType { grams = 1, kilograms = 2, millileters = 3, liters = 4, pinch = 5, teaspoon = 6, tablespoon = 7, whole = 8, Cup = 9 } public class ingredientWrapper { private ingredientType _t; public int value { get { return (int)_t; } set { _t = (ingredientType)value; } } public ingredientType EnumValue { get { return _t; } set { _t = value; } } public static implicit operator ingredientWrapper(ingredientType i) { return new ingredientWrapper { EnumValue = i }; } public static implicit operator ingredientType(ingredientWrapper iw) { return iw.EnumValue; } </code></pre> <p>It is deseralized and consumed as such on the client-side:</p> <pre><code> public async Task&lt;IEnumerable&lt;Recipe&gt;&gt; GetAll() { HttpResponseMessage response = await _Recipeclient.GetAsync(RecipeServiceUrl); var jsonString = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject&lt;Recipe[]&gt;(jsonString); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload