Skip to content

Instantly share code, notes, and snippets.

@oledid
Last active March 15, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oledid/01edc631fdf63b43fe223d6fa9c3651d to your computer and use it in GitHub Desktop.
Save oledid/01edc631fdf63b43fe223d6fa9c3651d to your computer and use it in GitHub Desktop.
Serialize LLBLGEN Entity to json without LLBLGEN-specific fields
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json;
using Your.ORM.EntityClasses; // <- replace
namespace Your.Namespace // <-
{
public static class CommonEntityBaseExtensions
{
/// <summary>
/// Serializes EntityFields as json (and ignores other LLBLGEN-specific fields)
/// </summary>
public static string ToJson(this CommonEntityBase entity)
{
if (entity == null)
return null;
dynamic expando = new ExpandoObject();
foreach (var field in entity.Fields)
{
((IDictionary<string, object>)expando).Add(field.Name, field.CurrentValue);
}
return JsonConvert.SerializeObject(expando);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment