Giter Site home page Giter Site logo

Comments (4)

xljiulang avatar xljiulang commented on May 25, 2024

[JsonPropertyName],因为KeyValue序列化也是使用system.text.json

from webapiclient.

ShmilyChen avatar ShmilyChen commented on May 25, 2024

如果我还有Form 的某个字段为 json 文本的离谱需求有相应的特性可以用来标注吗

from webapiclient.

xljiulang avatar xljiulang commented on May 25, 2024

参考 https://webapiclient.github.io/guide/deformed-interface.html#%E9%80%82%E9%85%8D%E7%95%B8%E5%BD%A2%E6%8E%A5%E5%8F%A3

from webapiclient.

ShmilyChen avatar ShmilyChen commented on May 25, 2024

跟着源码debug了一遍,大概看到是先通过System.Text.Json转换为Josn然后再转为请求参数,所以用了个转换器来折中的解决这个问题

实体类

/// <summary>
/// AccessToken输入参数
/// </summary>
public class AccessTokenInput
{
    /// <summary>
    /// 客户端ID
    /// </summary>
    [JsonPropertyName("client_id")]
    public string ClientId { get; set; }

    /// <summary>
    /// 访问内容
    /// </summary>
   [JsonConverter(typeof(ObjectToStringConverter))]
   public ChatContent Content { get; set; }
}

public record ChatContent(string content)

转换器

public class ObjectToStringConverter : JsonConverter<object>
{
    public override bool CanConvert(Type typeToConvert)
    {
        return typeToConvert.IsClass;
    }

    public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var serialize = JsonSerializer.Deserialize<string>(ref reader, options);
        return JsonSerializer.Deserialize(serialize, typeToConvert, options);
    }

    public override void Write(
        Utf8JsonWriter        writer,
        object                objectToWrite,
        JsonSerializerOptions options)
    {
        writer.WriteStringValue(JsonSerializer.Serialize(objectToWrite, options));
    }
}

声明接口

Task<string> SendMsg([FormContent] AccessTokenInput input);

from webapiclient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.