diff --git a/documentation/documentation/documents/json/newtonsoft.md b/documentation/documentation/documents/json/newtonsoft.md index 1c1484b4d2..b16c6defc2 100644 --- a/documentation/documentation/documents/json/newtonsoft.md +++ b/documentation/documentation/documents/json/newtonsoft.md @@ -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 JsonSerializer's. +## 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: +- `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]> + + diff --git a/src/Marten.Testing/Examples/ConfiguringDocumentStore.cs b/src/Marten.Testing/Examples/ConfiguringDocumentStore.cs index 34a124f650..36a6daf21b 100644 --- a/src/Marten.Testing/Examples/ConfiguringDocumentStore.cs +++ b/src/Marten.Testing/Examples/ConfiguringDocumentStore.cs @@ -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 diff --git a/src/Marten/StoreOptions.cs b/src/Marten/StoreOptions.cs index 1794e2692c..d2902917be 100644 --- a/src/Marten/StoreOptions.cs +++ b/src/Marten/StoreOptions.cs @@ -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 /// - /// + /// /// Casing style to be used in serialization /// Allow to set collection storage as raw arrays (without explicit types) /// Allow non public members to be used during deserialization public void UseDefaultSerialization( - EnumStorage enumStyle = EnumStorage.AsInteger, + EnumStorage enumStorage = EnumStorage.AsInteger, Casing casing = Casing.Default, CollectionStorage collectionStorage = CollectionStorage.Default, NonPublicMembersStorage nonPublicMembersStorage = NonPublicMembersStorage.Default @@ -230,7 +230,7 @@ public void UseDefaultSerialization( Serializer( new JsonNetSerializer { - EnumStorage = enumStyle, + EnumStorage = enumStorage, Casing = casing, CollectionStorage = collectionStorage, NonPublicMembersStorage = nonPublicMembersStorage