zoukankan      html  css  js  c++  java
  • Invoke microsoft own System.Text.Json.JsonSerialize() method to serialize object,without encoding

    1.install-package system.text.json;

    2.

    using System.Text.Json;

    using System.IO;

    3.

     1 static void SystemTextJsonDemo()
     2         {
     3             var obj = new[]
     4             {
     5                 new
     6                 {
     7                     Id=1,
     8                     Name="Fred",
     9                     Age=10
    10                 },
    11                 new
    12                 {
    13                     Id=2,
    14                     Name="Fred2",
    15                     Age=20
    16                 },
    17                 new
    18                 {
    19                     Id=3,
    20                     Name="Fred3",
    21                     Age=30
    22                 }
    23             };
    24             JsonSerializerOptions jso = new JsonSerializerOptions();
    25             jso.WriteIndented = true;            
    26             string jsonValue = JsonSerializer.Serialize(obj, typeof(object), jso);
    27             File.WriteAllText("JsonText2.json", jsonValue);
    28             Console.WriteLine(jsonValue);
    29         }

    Please pay attention to the indented parts.Such as the JsonConvert.Serialize(obj,Formating.Indented);

    When you want to show the original plain text as defined. Replate the encoder with belowing.

    1 jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;

    The result illustrated the fact.

     As the declaration of below,The UnsafeRelaxedJsonEscaping option.

    //
    // Summary:
    // Gets a built-in JavaScript encoder instance that is less strict about what is
    // encoded.
    //
    // Returns:
    // A JavaScript encoder instance.
    public static JavaScriptEncoder UnsafeRelaxedJsonEscaping { get; }
  • 相关阅读:
    wordpress调用函数大全
    Dedecms 数据库结构分析
    屏幕广播的实现(二)
    屏幕广播的实现(一)
    Alt+Ctrl+Del组合键的屏蔽
    关于钩子(HOOK)
    C# 线程入门 00
    C# 中 关键字 return break continue 详解
    Windows 网络命令
    vue自定义指令封装(加深印象)
  • 原文地址:https://www.cnblogs.com/Fred1987/p/14485400.html
Copyright © 2011-2022 走看看