Skip to content

Commit

Permalink
Added documentation of NonPublicMembersStorage, added also missing do…
Browse files Browse the repository at this point in the history
…cumentation of other Newtonsoft.NET Serializer options
  • Loading branch information
oskardudycz committed Nov 2, 2019
1 parent 1afd895 commit be74783
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
47 changes: 47 additions & 0 deletions documentation/documentation/documents/json/newtonsoft.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,50 @@ Marten actually has to keep two Newtonsoft.Json serializers, with one being a "c
the customization is done with a nested closure so that the same configuration is always applied to both internal <code>JsonSerializer's</code>.
</div>

## Enum Storage

Marten allows how enum values are being stored. By default, they are stored as integers but it is possible to change that to storing them as strings.

To do that you need to change the serialization settings in the `DocumentStore` options.

<[sample:customize_json_net_enum_storage_serialization]>

## Fields Names Casing

Marten by default stores field names "as they are" (C# naming convention is PascalCase for public properties).

You can have them also automatically formatted to:
- `CamelCase`,
- `snake_case`
by changing the serialization settings in the `DocumentStore` options.

<[sample:customize_json_net_camelcase_casing_serialization]>

<[sample:customize_json_net_snakecase_casing_serialization]>

## Collection Storage

Marten by default stores the collections as strongly typed (so with $type and $value). Because of that and current `MartenQueryable` limitations, it might result in not properly resolved nested collections queries.

You can change collection storage to `AsArray` then custom `JsonConverter` that will store:

This comment has been minimized.

Copy link
@mysticmind

mysticmind Nov 3, 2019

Member

Suggestion to tweak to the sentence as, "Changing the collection storage to AsArray using a custom JsonConverter will store it as regular JSON array for the following:

  • lCollection<>
  • ...
- `ICollection<>`,
- `IList<>`,
- `IReadOnlyCollection<>`,
- `IEnumerable<>`
as regular JSON array.

That improves the nested collections queries handling.

To do that you need to change the serialization settings in the `DocumentStore` options.

<[sample:customize_json_net_snakecase_collectionstorage]>

## Non Public Members Storage

By default `Newtonsoft.Json` only deserializes properties with public setters.

You can allow deserialisation of properties with non-public setters by changing the serialization settings in the `DocumentStore` options.

<[sample:customize_json_net_snakecase_nonpublicmembersstorage_nonpublicsetters]>


75 changes: 75 additions & 0 deletions src/Marten.Testing/Examples/ConfiguringDocumentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,81 @@ public void customize_json_net_serialization()
// ENDSAMPLE
}

public void customize_json_net_enum_storage_serialization()
{
// SAMPLE: customize_json_net_enum_storage_serialization

var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");

// Replace the default JsonNetSerializer default enum storage
// with storing them as string
_.UseDefaultSerialization(enumStorage: EnumStorage.AsString);
});
// ENDSAMPLE
}

public void customize_json_net_camelcase_casing_serialization()
{
// SAMPLE: customize_json_net_camelcase_casing_serialization

var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");

// Replace the default (as is) JsonNetSerializer field names casing
// with camelCase formatting
_.UseDefaultSerialization(casing: Casing.CamelCase);
});
// ENDSAMPLE
}

public void customize_json_net_snakecase_casing_serialization()
{
// SAMPLE: customize_json_net_snakecase_casing_serialization

var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");

// Replace the default (as is) JsonNetSerializer field names casing
// with snake_case formatting
_.UseDefaultSerialization(casing: Casing.SnakeCase);
});
// ENDSAMPLE
}

public void customize_json_net_snakecase_collectionstorage()
{
// SAMPLE: customize_json_net_snakecase_collectionstorage

var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");

// Replace the default (strongly typed) JsonNetSerializer collection storage
// with JSON array formatting
_.UseDefaultSerialization(collectionStorage: CollectionStorage.AsArray);
});
// ENDSAMPLE
}

public void customize_json_net_snakecase_nonpublicmembersstorage_nonpublicsetters()
{
// SAMPLE: customize_json_net_snakecase_nonpublicmembersstorage_nonpublicsetters

var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");

// Replace the default (only public setters) JsonNetSerializer deserialization settings
// with allowing to also deserialize using non-public setters
_.UseDefaultSerialization(nonPublicMembersStorage: NonPublicMembersStorage.NonPublicSetters);
});
// ENDSAMPLE
}

public void setting_event_schema()
{
// SAMPLE: setting_event_schema
Expand Down
6 changes: 3 additions & 3 deletions src/Marten/StoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ public void Serializer(ISerializer serializer)
/// Use the default serialization (ilmerged Newtonsoft.Json) with Enum values
/// stored as either integers or strings
/// </summary>
/// <param name="enumStyle"></param>
/// <param name="enumStorage"></param>
/// <param name="casing">Casing style to be used in serialization</param>
/// <param name="collectionStorage">Allow to set collection storage as raw arrays (without explicit types)</param>
/// <param name="nonPublicMembersStorage">Allow non public members to be used during deserialization</param>
public void UseDefaultSerialization(
EnumStorage enumStyle = EnumStorage.AsInteger,
EnumStorage enumStorage = EnumStorage.AsInteger,
Casing casing = Casing.Default,
CollectionStorage collectionStorage = CollectionStorage.Default,
NonPublicMembersStorage nonPublicMembersStorage = NonPublicMembersStorage.Default
Expand All @@ -230,7 +230,7 @@ public void UseDefaultSerialization(
Serializer(
new JsonNetSerializer
{
EnumStorage = enumStyle,
EnumStorage = enumStorage,
Casing = casing,
CollectionStorage = collectionStorage,
NonPublicMembersStorage = nonPublicMembersStorage
Expand Down

0 comments on commit be74783

Please sign in to comment.