5

我注意到,当我序列化具有 HTTP_VERB 键的 C# 字典时,它会变成JSON 结构中的httP_VERB而不是 hTTP_VERB 或 http_verb,我预计骆驼案例会成功。

这是我用来重现问题的代码:

  class Program {
    static void Main(string[] args) {

      var settings = new JsonSerializerSettings();

      settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
      settings.NullValueHandling = NullValueHandling.Ignore;

      var headers = new Dictionary<string, string>();
      headers["SessionID"] = "123456";
      headers["HTTP_VERB"] = "POST";
      headers["HTTPVERSION"] = "1";
      var data = new
      {
        headers = headers
      };

      string serializedEvent = JsonConvert.SerializeObject(data, settings);

      if (serializedEvent.Contains("httP_VERB")) {
        Console.WriteLine("Something is wrong with this camel case");
      }
      else {
        Console.WriteLine("Sucess");
      }

    }
  }
4

3 回答 3

12

无法按照CamelCasePropertyNamesContractResolver您想要的方式将字符串转换为驼峰式大小写,但您可以轻松编写自己的ContractResolver.

我在我的CsCss项目中使用了 PascalCase 转换。稍作调整后,结果如下:

public enum IdentifierCase
{
    Camel,
    Pascal,
}

public class CustomPropertyNamesContractResolver : DefaultContractResolver
{
    private static readonly CultureInfo Culture = CultureInfo.InvariantCulture;

    public CustomPropertyNamesContractResolver (bool shareCache = false)
        : base(shareCache)
    {
        Case = IdentifierCase.Camel;
        PreserveUnderscores = true;
    }

    public IdentifierCase Case { get; set; }
    public bool PreserveUnderscores { get; set; }

    protected override string ResolvePropertyName (string propertyName)
    {
        return ChangeCase(propertyName);
    }

    private string ChangeCase (string s)
    {
        var sb = new StringBuilder(s.Length);

        bool isNextUpper = Case == IdentifierCase.Pascal, isPrevLower = false;
        foreach (var c in s) {
            if (c == '_') {
                if (PreserveUnderscores)
                    sb.Append(c);
                isNextUpper = true;
            }
            else {
                sb.Append(isNextUpper ? char.ToUpper(c, Culture) : isPrevLower ? c : char.ToLower(c, Culture));
                isNextUpper = false;
                isPrevLower = char.IsLower(c);
            }
        }
        return sb.ToString();
    }

    // Json.NET implementation for reference
    private static string ToCamelCase (string s)
    {
        if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0]))
            return s;
        var sb = new StringBuilder();
        for (int i = 0; i < s.Length; ++i) {
            if (i == 0 || i + 1 >= s.Length || char.IsUpper(s[i + 1]))
                sb.Append(char.ToLower(s[i], Culture));
            else {
                sb.Append(s.Substring(i));
                break;
            }
        }
        return sb.ToString();
    }
}

该转换器同时支持PascalCasecamelCase。它似乎以您期望的方式转换属性名称。

我已经ToCamelCase从 Json.NET 中留下了原始函数以供参考。

示例程序:

internal class Program
{
    private static void Main ()
    {
        var obj = new Dictionary<string, string> {
            { "SessionID", "123456" },
            { "HTTP_VERB", "POST" },
            { "HTTPVERSION", "1" },
        };
        var settings = new JsonSerializerSettings {
            Formatting = Formatting.Indented,
            ContractResolver = new CustomPropertyNamesContractResolver()
        };
        string strCamel = JsonConvert.SerializeObject(obj, settings);
        Console.WriteLine("camelCase: \n" + strCamel);
        Console.WriteLine(strCamel.Contains("httP_VERB") ? "Something is wrong with this camel case" : "Success");
        settings.ContractResolver = new CustomPropertyNamesContractResolver {
            Case = IdentifierCase.Pascal,
            PreserveUnderscores = false,
        };
        string strPascal = JsonConvert.SerializeObject(obj, settings);
        Console.WriteLine("PascalCase: \n" + strPascal);
        Console.ReadKey();
    }
}

输出:

camelCase:
{
  "sessionId": "123456",
  "http_Verb": "POST",
  "httpversion": "1"
}
Success
PascalCase:
{
  "SessionId": "123456",
  "HttpVerb": "POST",
  "Httpversion": "1"
}
于 2013-09-16T12:30:05.333 回答
2

我需要以相反的顺序解析,即 JSON 中的小写下划线属性到 .NET 中的帕斯卡非下划线属性。Discord 的解决方案帮助我做到了这一点:

public class PascalCasePropertyNamesContractResolver : DefaultContractResolver
{
    private static readonly CultureInfo Culture = CultureInfo.InvariantCulture;

    public PascalCasePropertyNamesContractResolver(bool shareCache = false)
        : base(shareCache)
    {
    }

    protected override string ResolvePropertyName(string s)
    {
        var sb = new StringBuilder(s.Length);

        bool isNextUpper = false, isPrevLower = false;

        for (var i = 0; i < s.Length; i++)
        {
            var c = s[i];

            sb.Append(char.ToLower(c, Culture));

            isNextUpper = i + 1 < s.Length && char.IsUpper(s[i + 1]);

            if (isNextUpper && isPrevLower)
            {
                sb.Append("_");
            }

            isPrevLower = char.IsLower(c);
        }

        return sb.ToString();
    }
}
于 2015-05-26T11:02:45.160 回答
1

这对我有帮助。

var contractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
contractResolver.NamingStrategy = new Newtonsoft.Json.Serialization.SnakeCaseNamingStrategy();
settings.ContractResolver = contractResolver;
dynamic deserializedObject = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(requestResult.Content, settings);

您也可以尝试 CamelCaseNamingStrategy() 而不是 SnakeCaseNamingStrategy();

于 2020-11-26T09:35:03.087 回答