Example #1
0
        private static void UseJsonDotNetWithSerializerOptions(Joke joke)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                // CamelCase für alle Properties
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
            };

            // Enums als String und nicht den numerischen Wert serialisieren
            settings.Converters.Add(new StringEnumConverter(camelCaseText: true));

            string jokeAsJson = JsonConvert.SerializeObject(joke, settings);

            Console.WriteLine(jokeAsJson);
            Joke parsedJoke = JsonConvert.DeserializeObject <Joke>(jokeAsJson);
        }
Example #2
0
        static void Main(string[] args)
        {
            var joke = new Joke
            {
                Id              = 1,
                Text            = "Chuck Norris can make a class that is both abstract and final.",
                Categories      = new[] { JokeCategories.Nerdy, JokeCategories.Classic },
                Rating          = 5,
                AddedToLibrary  = DateTime.Now,
                TopSecretNotice = "Chuck worked for the NSA"
            };

            UseJavaScriptSerializer(joke);
            UseJsonDotNetWithoutSerializerOptions(joke);
            UseJsonDotNetWithSerializerOptions(joke);
        }