ETG.ExpressionJsonSerializer 1.0.0.18

dotnet add package ETG.ExpressionJsonSerializer --version 1.0.0.18
                    
NuGet\Install-Package ETG.ExpressionJsonSerializer -Version 1.0.0.18
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ETG.ExpressionJsonSerializer" Version="1.0.0.18" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ETG.ExpressionJsonSerializer" Version="1.0.0.18" />
                    
Directory.Packages.props
<PackageReference Include="ETG.ExpressionJsonSerializer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ETG.ExpressionJsonSerializer --version 1.0.0.18
                    
#r "nuget: ETG.ExpressionJsonSerializer, 1.0.0.18"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ETG.ExpressionJsonSerializer@1.0.0.18
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ETG.ExpressionJsonSerializer&version=1.0.0.18
                    
Install as a Cake Addin
#tool nuget:?package=ETG.ExpressionJsonSerializer&version=1.0.0.18
                    
Install as a Cake Tool

ETG.ExpressionJsonSerializer

A powerful .NET library for serializing and deserializing LINQ Expression Trees to and from JSON format.

NuGet License

Overview

ETG.ExpressionJsonSerializer allows you to convert complex LINQ Expression Trees into JSON strings and reconstruct them back into executable expressions. This is particularly useful for:

  • Storing expressions in databases
  • Transmitting expressions over network protocols
  • Caching compiled expressions
  • Building dynamic query builders
  • Creating rule engines with serializable logic

Installation

Install the package via NuGet Package Manager:

dotnet add package ETG.ExpressionJsonSerializer

Or via Package Manager Console:

Install-Package ETG.ExpressionJsonSerializer

Supported Frameworks

  • .NET Framework 4.7.2, 4.8, 4.8.1
  • .NET Standard 2.0, 2.1
  • .NET 8.0

Features

  • ✅ Complete Expression Support: Binary, Unary, Member Access, Method Calls, Conditionals, Lambda, and more
  • ✅ Array Operations: Array indexing and length operations
  • ✅ Type Conversions: Convert, TypeAs, and TypeIs expressions
  • ✅ Null Coalescing: Support for null-coalescing operator (??)
  • ✅ Logical & Bitwise Operations: AND, OR, XOR, NOT operations
  • ✅ Comparison Operations: Greater than, less than, equal, not equal
  • ✅ Arithmetic Operations: Add, subtract, multiply, divide, modulo
  • ✅ String Operations: Concatenation and string methods
  • ✅ LINQ Operations: Where, Select, OrderBy, and other LINQ methods

Usage

Basic Serialization

using ETG.ExpressionJsonSerializer;
using System;
using System.Linq.Expressions;

// Create an expression
Expression<Func<int, int, int>> addExpression = (a, b) => a + b;

// Create serializer
var serializer = new ExpressionJsonSerializer();

// Serialize to JSON
string json = serializer.Serialize(addExpression);

// Deserialize back to expression
var deserializedExpression = serializer.Deserialize<Expression<Func<int, int, int>>>(json);

// Compile and execute
var result = deserializedExpression.Compile()(5, 3);
Console.WriteLine(result); // Output: 8

Complex Expression Example

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string[] Hobbies { get; set; }
}

// Create a complex expression
Expression<Func<Person, bool>> filterExpression = 
    p => p.Age > 18 && p.Name.StartsWith("J") && p.Hobbies.Length > 0;

var serializer = new ExpressionJsonSerializer();

// Serialize
string json = serializer.Serialize(filterExpression);

// Store in database, send over network, etc.
// ...

// Deserialize and use
var restored = serializer.Deserialize<Expression<Func<Person, bool>>>(json);
var compiled = restored.Compile();

var person = new Person 
{ 
    Name = "John", 
    Age = 25, 
    Hobbies = new[] { "Reading", "Gaming" } 
};

bool matches = compiled(person); // Returns: true

Using with Newtonsoft.Json

The library uses Newtonsoft.Json internally. You can customize serialization settings:

using Newtonsoft.Json;

var settings = new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    TypeNameHandling = TypeNameHandling.Auto
};

var serializer = new ExpressionJsonSerializer();
string json = serializer.Serialize(expression, settings);

LINQ Query Example

using System.Collections.Generic;
using System.Linq;

var people = new List<Person>
{
    new Person { Name = "Alice", Age = 30 },
    new Person { Name = "Bob", Age = 25 },
    new Person { Name = "Charlie", Age = 35 }
};

// Create a LINQ query expression
Expression<Func<IEnumerable<Person>, IEnumerable<Person>>> queryExpression = 
    list => list.Where(p => p.Age > 26).OrderBy(p => p.Name);

var serializer = new ExpressionJsonSerializer();

// Serialize
string json = serializer.Serialize(queryExpression);

// Deserialize and execute
var restored = serializer.Deserialize<Expression<Func<IEnumerable<Person>, IEnumerable<Person>>>>(json);
var result = restored.Compile()(people);

foreach (var person in result)
{
    Console.WriteLine($"{person.Name} - {person.Age}");
}
// Output:
// Alice - 30
// Charlie - 35

Supported Expression Types

Expression Type Supported Example
Binary ✅ a + b, a > b, a && b
Unary ✅ !flag, -value
Member Access ✅ obj.Property
Method Call ✅ obj.Method()
Conditional ✅ condition ? a : b
Lambda ✅ x => x + 1
Constant ✅ 42, "text"
Array Index ✅ array[0]
Type Conversion ✅ (int)value
Type Check ✅ obj is string
Type As ✅ obj as string
Null Coalescing ✅ value ?? default
New Object ✅ new Person()
Invocation ✅ func()

Real-World Use Cases

1. Dynamic Filtering System

// Store user-defined filters in database as JSON
public class SavedFilter
{
    public string Name { get; set; }
    public string ExpressionJson { get; set; }
}

// Save a filter
var filter = (Expression<Func<Product, bool>>)(p => p.Price < 100 && p.InStock);
var savedFilter = new SavedFilter
{
    Name = "Affordable In-Stock Items",
    ExpressionJson = serializer.Serialize(filter)
};
database.SavedFilters.Add(savedFilter);

// Later, load and apply the filter
var loadedFilter = database.SavedFilters.First(f => f.Name == "Affordable In-Stock Items");
var expression = serializer.Deserialize<Expression<Func<Product, bool>>>(loadedFilter.ExpressionJson);
var affordableProducts = products.Where(expression.Compile());

2. Rule Engine

public class BusinessRule
{
    public string RuleName { get; set; }
    public string ConditionJson { get; set; }
}

// Define a rule
Expression<Func<Order, bool>> discountRule = 
    order => order.TotalAmount > 1000 && order.CustomerType == "Premium";

var rule = new BusinessRule
{
    RuleName = "Premium Discount Eligibility",
    ConditionJson = serializer.Serialize(discountRule)
};

// Evaluate rule
var condition = serializer.Deserialize<Expression<Func<Order, bool>>>(rule.ConditionJson);
bool isEligible = condition.Compile()(currentOrder);

3. Remote Query Execution

// Client side
Expression<Func<User, bool>> query = u => u.IsActive && u.Country == "USA";
string jsonQuery = serializer.Serialize(query);

// Send to server via API
var response = await httpClient.PostAsync("/api/users/query", 
    new StringContent(jsonQuery, Encoding.UTF8, "application/json"));

// Server side
[HttpPost("query")]
public IActionResult QueryUsers([FromBody] string expressionJson)
{
    var serializer = new ExpressionJsonSerializer();
    var expression = serializer.Deserialize<Expression<Func<User, bool>>>(expressionJson);
    var results = _context.Users.Where(expression.Compile()).ToList();
    return Ok(results);
}

Performance Considerations

  • Serialization: Fast for most expressions; complex nested expressions may take longer
  • Deserialization: Reconstruction of expression trees is efficient
  • Caching: Consider caching compiled expressions to avoid repeated deserialization

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Repository

https://bitbucket.org/sabentis/expression-json-serializer

License

This project is licensed under the MIT License.

Support

For issues, questions, or feature requests, please open an issue on the repository.

Credits

Developed and maintained by Easy Tech Global.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.18 145 2/16/2026
1.0.0.17 1,283 2/16/2026