3

我们有一个系统,其中用 C# 编写的服务器实现 REST 接口,客户端用 JS 编写,大部分数据以 JSON 形式传递。

在某种程度上,camelCase 和 PascalCase 世界之间存在冲突。

参数主要来自服务器中的名称,并且都在 PascalCase 中。

JS 前端是使用 camelCase 编写的,并且由于 JSON 来自 JS 世界,因此期望服务器将接受这样的参数。

什么是可接受的解决方案,为什么?

4

2 回答 2

1

对于 Web API 方法的参数使用“camelCase”,我认为这是.NET 中方法参数的建议命名约定

对于属性,使用JSON 序列化属性

public class Person
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

序列化对象将如下所示:

{ "id": 10, "name": "My Name" }

使用这种方法,您的 C# 代码将使用 PascalCase,而客户端(Javascript)将使用 camelCase。

于 2016-10-02T23:42:33.653 回答
0

我不知道这是否是完整的答案,因为尚不清楚您到底想达到什么目标。

显然 C# 是 Pascal 案例,JSON 是 Camel 案例。

因此,对于我们的 Web ASP.Net Web API 应用程序,我们已经为 Json.NET 实现了 DataConverter:

public class DataConverter : JsonConverter
{
    #region Overriding
    public override bool CanRead
    {
        get { return true; }
    }
    public override bool CanWrite
    {
        get { return true; }
    }
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return this.ReadValue(reader);
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        this.WriteValue(writer, value, serializer);
    }
    #endregion

    #region Assistants
    private object ReadValue(JsonReader reader)
    {
        while (reader.TokenType == JsonToken.Comment)
        {
            if (reader.Read() == false)
                throw new Exception("Unexpected end.");
        }

        switch (reader.TokenType)
        {
            case JsonToken.StartObject:
                return this.ReadObject(reader);

            case JsonToken.StartArray:
                return this.ReadList(reader);

            default:
                if (this.CheckPrimitive(reader.TokenType) == true)
                    return reader.Value;

                throw new Exception(string.Format(CultureInfo.InvariantCulture, "Unexpected token when converting ExpandoObject: {0}", reader.TokenType));
        }
    }
    private object ReadList(JsonReader reader)
    {
        List<object> collection = new List<object>();

        while (reader.Read() == true)
        {
            switch (reader.TokenType)
            {
                case JsonToken.Comment:
                    break;

                case JsonToken.EndArray:
                    return collection;

                default:
                    object value = this.ReadValue(reader);

                    collection.Add(value);
                    break;
            }
        }

        throw new Exception("Unexpected end.");
    }
    private object ReadObject(JsonReader reader)
    {
        IDictionary<string, object> expando = new ExpandoObject();

        while (reader.Read() == true)
        {
            switch (reader.TokenType)
            {
                case JsonToken.PropertyName:
                    string property = reader.Value.ToString().ToCase(Casing.Pascal);

                    if (reader.Read() == false)
                        throw new Exception("Unexpected end.");

                    object value = this.ReadValue(reader);

                    expando[property] = value;
                    break;

                case JsonToken.Comment:
                    break;

                case JsonToken.EndObject:
                    return expando;
            }
        }

        throw new Exception("Unexpected end.");
    }

    private void WriteValue(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (this.CheckPrimitive(value) == true)
        {
            writer.WriteValue(value);
            return;
        }

        if (value is Guid)
        {
            this.WriteValue(writer, (Guid)value, serializer);
            return;
        }

        if (value is MyType)
        {
            this.WriteValue(writer, (MyType)value, serializer);
            return;
        }

        if (value is IDynamicMetaObjectProvider && value is IDictionary<string, object>)
        {
            this.WriteObject(writer, (IDictionary<string, object>)value, serializer);
            return;
        }

        if (value is IEnumerable)
        {
            IEnumerable enumerable = value as IEnumerable;
            this.WriteArray(writer, enumerable, serializer);
            return;
        }

        this.WriteObject(writer, value, serializer);
    }
    private void WriteValue(JsonWriter writer, Guid guid, JsonSerializer serializer)
    {
        writer.WriteValue(guid.ToString());
    }
    private void WriteValue(JsonWriter writer, MyType myType, JsonSerializer serializer)
    {
        writer.WriteValue(myType.ToString());
    }
    private void WriteArray(JsonWriter writer, IEnumerable enumerable, JsonSerializer serializer)
    {
        writer.WriteStartArray();
        foreach (object value in enumerable)
        {
            this.WriteValue(writer, value, serializer);
        }
        writer.WriteEndArray();
    }
    private void WriteObject(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteStartObject();
        foreach (PropertyInfo properties in value.GetType().GetProperties())
        {
            ParameterInfo[] parameters = properties.GetGetMethod().GetParameters();
            if (parameters.Length == 0)
            {
                writer.WritePropertyName(properties.Name.ToCase(Casing.Camel));

                this.WriteValue(writer, properties.GetValue(value), serializer);
            }
        }
        writer.WriteEndObject();
    }
    private void WriteObject(JsonWriter writer, IDictionary<string, object> value, JsonSerializer serializer)
    {
        writer.WriteStartObject();
        foreach (KeyValuePair<string, object> properties in value)
        {
            writer.WritePropertyName(properties.Key.ToCase(Casing.Camel)); // Implement own casing...

            this.WriteValue(writer, properties.Value, serializer);
        }
        writer.WriteEndObject();
    }

    private bool CheckPrimitive(JsonToken token)
    {
        switch (token)
        {
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            case JsonToken.Boolean:
            case JsonToken.Null:
            case JsonToken.Undefined:
            case JsonToken.Date:
            case JsonToken.Bytes:
                return true;
        }
        return false;
    }
    private bool CheckPrimitive(object value)
    {
        if (value == null)
            return true;

        if (value is bool)
            return true;

        if (value is byte)
            return true;

        if (value is sbyte)
            return true;

        if (value is short)
            return true;

        if (value is ushort)
            return true;

        if (value is int)
            return true;

        if (value is uint)
            return true;

        if (value is long)
            return true;

        if (value is ulong)
            return true;

        if (value is float)
            return true;

        if (value is double)
            return true;

        if (value is decimal)
            return true;

        if (value is char)
            return true;

        if (value is string)
            return true;

        if (value is DateTime)
            return true;

        if (value is Enum)
            return true;

        return false;
    }
    #endregion
}

当 JSON 转换为 C# 对象时,它会将“someProperty”转换为“SomeProperty”,反之则将“SomeProperty”转换为“someProperty”。它允许我们在 API 控制器中使用动态关键字(对于 ASP.Net Core 项目,输入参数需要属性 [FromBody])。

于 2016-10-02T23:28:04.880 回答