Exemple #1
0
        public void customize_json_net_serialization()
        {
            // SAMPLE: customize_json_net_serialization
            var serializer = new Marten.Services.JsonNetSerializer();

            // To change the enum storage policy to store Enum's as strings:
            serializer.EnumStorage = EnumStorage.AsString;

            // All other customizations:
            serializer.Customize(_ =>
            {
                // Code directly against a Newtonsoft.Json JsonSerializer
                _.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
                _.ConstructorHandling  = ConstructorHandling.AllowNonPublicDefaultConstructor;
            });

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

                // Replace the default JsonNetSerializer with the one we configured
                // above
                _.Serializer(serializer);
            });
            // ENDSAMPLE
        }
        private static Marten.DocumentStore CreateNewStore(string connString, DocumentMappableSerializationBinder serializationBinder,
                                                           IDocumentStoreAssemblyDiscoverer discoverer)
        {
            var store = Marten.DocumentStore.For(options =>
            {
                options.Logger(new SerilogMartenLogger());

                var serializer = new Marten.Services.JsonNetSerializer
                {
                    EnumStorage = EnumStorage.AsString
                };

                serializer.Customize(x => x.SerializationBinder = serializationBinder);

                options.PLV8Enabled = false; // Not installed by default in PostgreSQL 11

                options.AutoCreateSchemaObjects = AutoCreate.None;

                options.Connection(connString);

                options.Serializer(serializer);

                foreach (var(docType, attr) in discoverer.DiscoverAutoDocumentCollectionTypes())
                {
                    options.Storage.MappingFor(docType);
                }

                foreach (var cfgType in discoverer.DiscoverDocumentStoreConfigurers())
                {
                    ((IDocumentStoreConfiguration)Activator.CreateInstance(cfgType)).Configure(options);
                }
            });

            return(store);
        }
        public static QueryPlan ExplainQuery(this IManagedConnection runner, NpgsqlCommand cmd, Action <IConfigureExplainExpressions> configureExplain = null)
        {
            var serializer = new JsonNetSerializer();

            var config = new ConfigureExplainExpressions();

            configureExplain?.Invoke(config);

            cmd.CommandText = string.Concat($"explain ({config} format json) ", cmd.CommandText);
            using (var reader = runner.ExecuteReader(cmd))
            {
                var queryPlans   = reader.Read() ? serializer.FromJson <QueryPlanContainer[]>(reader.GetTextReader(0)) : null;
                var planToReturn = queryPlans?[0].Plan;
                if (planToReturn != null)
                {
                    planToReturn.PlanningTime  = queryPlans[0].PlanningTime;
                    planToReturn.ExecutionTime = queryPlans[0].ExecutionTime;
                    planToReturn.Command       = cmd;
                }
                return(planToReturn);
            }
        }
        public void customize_json_net_serialization()
        {
            // SAMPLE: customize_json_net_serialization
    var serializer = new Marten.Services.JsonNetSerializer();
            
    // To change the enum storage policy to store Enum's as strings:
    serializer.EnumStorage = EnumStorage.AsString;

    // All other customizations:
    serializer.Customize(_ =>
    {
        // Code directly against a Newtonsoft.Json JsonSerializer
        _.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
        _.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
    });

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

        // Replace the default JsonNetSerializer with the one we configured
        // above
        _.Serializer(serializer);
    });
            // ENDSAMPLE
        }