Table of Contents

Class ViewModelPropertyFilter

Namespace
Dynamicweb.Rendering
Assembly
Dynamicweb.Core.dll

Limits which properties of a ViewModelBase are written when the view model is serialized to JSON. Attach it to a view model with SetPropertyFilter(ViewModelBase, ViewModelPropertyFilter?); it is propagated to nested view models during serialization, so setting it on a root view model is enough to shape the entire response.

public sealed class ViewModelPropertyFilter
Inheritance
ViewModelPropertyFilter
Inherited Members

Remarks

The filter only removes properties from the JSON output — it never exposes additional data, and it does not affect how the view model is built or how it can be used in templates. A view model type that is not mentioned in the filter is serialized in full. Type and property names are matched case-insensitively, and a type entry also matches subclasses (e.g. an entry for ProductViewModel applies to extended product view models). For the filter to take effect, Modify(JsonTypeInfo) must be registered as a type info modifier on the JsonSerializerOptions used for serialization.

Methods

BindRootType(Type)

Binds the bare (not type scoped) property names from the specification to the given root view model type. Has no effect if the specification contained no bare names or the filter has already been bound.

public void BindRootType(Type rootType)

Parameters

rootType Type

The type of the root view model being serialized.

Modify(JsonTypeInfo)

A JsonTypeInfo modifier that makes serialization honor the filter attached to a view model via SetPropertyFilter(ViewModelBase, ViewModelPropertyFilter?). Register it with JsonTypeInfoResolver.WithAddedModifier on the serializer options. Filtered-out properties are skipped without invoking their getters, so lazy-loaded values are never evaluated.

public static void Modify(JsonTypeInfo jsonTypeInfo)

Parameters

jsonTypeInfo JsonTypeInfo

The type info being configured.

Parse(string?)

Parses a filter specification into a ViewModelPropertyFilter.

public static ViewModelPropertyFilter? Parse(string? specification)

Parameters

specification string

A comma separated list of segments. A bare property name (e.g. Name,Email) applies to the root view model type once BindRootType(Type) has been called. A type scoped segment (e.g. UserViewModel[Name,Email]) applies to all view models of that type anywhere in the object graph.

Returns

ViewModelPropertyFilter

The parsed filter, or null if the specification is empty or contains no names.

ShouldSerializeProperty(Type, string)

Determines whether the specified property should be written when serializing a view model of the specified type.

public bool ShouldSerializeProperty(Type viewModelType, string propertyName)

Parameters

viewModelType Type

The type of the view model being serialized.

propertyName string

The JSON name of the property.

Returns

bool

true if the property should be serialized; otherwise false.

To top