Note that there are some explanatory texts on larger screens.

plurals
  1. POJsonConverter: Serialize c# object to json object
    primarykey
    data
    text
    <p>When using Json.NET to serialize a MVC view model to json, I have a generic object property on my view model (<code>public object Result { get; set;}</code>) that is getting serialized to key-value pairs instead of an actual json object. Is there a converter I can use to force it to be serialized properly?</p> <p>This is what is currently being output by Json.NET:</p> <pre><code>"result": [ { "key": "AssetId", "value": "b8d8fb71-2553-485b-91bf-14c6c563d78b" }, { "key": "SearchResultType", "value": "Assets" }, { "key": "Name", "value": "abstract-q-c-1920-1920-8" } ] </code></pre> <p>Instead, I would want it to output this:</p> <pre><code>"result": { "AssetId": "b8d8fb71-2553-485b-91bf-14c6c563d78b", "SearchResultType": "Assets", "Name": "abstract-q-c-1920-1920-8" } </code></pre> <p><strong>EDIT</strong>: To answer the question of how that property is getting populated, it is via a RavenDB index:</p> <pre><code>public class SiteSearchIndexTask : AbstractMultiMapIndexCreationTask&lt;SiteSearchResult&gt; { public class Result { public object[] Content { get; set; } } public override string IndexName { get { return "SiteSearch/All"; } } public SiteSearchIndexTask() { AddMap&lt;Asset&gt;(items =&gt; from item in items where item.IsDeleted == false select new { Id = item.Id.ToString(), ObjectId = item.Id, ResultType = SearchResultType.Assets, Title = item.Name.Boost(3), Tags = item.Tags.Select(x =&gt; x.Name).Boost(2), Result = (object)item, Query = string.Join(" ", item.Description) }); AddMap&lt;User&gt;(items =&gt; from item in items where item.IsDeleted == false select new { Id = item.Id, ObjectId = item.UserId, ResultType = SearchResultType.Users, Title = item.Username.Boost(3), Tags = (BoostedValue) null, Result = (object)item, Query = string.Join(" ", item.FullName, item.Email) }); Store(x =&gt; x.ObjectId, FieldStorage.Yes); Store(x =&gt; x.ResultType, FieldStorage.Yes); Store(x =&gt; x.Title, FieldStorage.Yes); Store(x =&gt; x.Tags, FieldStorage.Yes); Store(x =&gt; x.Result, FieldStorage.Yes); Store(x =&gt; x.Query, FieldStorage.Yes); } } </code></pre> <p><strong>Edit 2</strong> Here are the Asset and User models (truncated for brevity, since they're just a bunch of auto properties)</p> <pre><code>public class Asset : IHasId { public string Id { get; set; } public Guid AssetId { get { Guid assetId; Guid.TryParse((Id ?? string.Empty).Replace("assets/", ""), out assetId); return assetId; } set { Id = "assets/" + value; } } public string Name { get; set; } public string Description { get; set; } } public class User : IHasId { public User() { Status = UserStatus.Active; } public string Id { get; set; } public int UserId { get { int userId; int.TryParse((Id ?? string.Empty).Replace("users/", ""), out userId); return userId; } set { Id = "users/" + value; } } public string Username { get; set; } public string Password { get; set; } public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public UserStatus Status { get; set; } } </code></pre> <p><strong>Edit 3</strong> As it turns out, when I step through the code, that object is actually of the type Raven.Abstractions.Linq.DynamicJsonObject, which contains an array of key-value pairs. So I guess this question might be more related to Raven than Json.NET. Unless of course there is a converter to go from key-value pair to json object.</p>
    singulars
    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.
 

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