标签: json-serialization

网络核心Web API JSON序列化-需要以$开头的字段

我正在使用Net Core Web API,需要返回属性名称为“ $ skip”的有效负载。我尝试使用DataAnnotations:

public class ApiResponseMessage
{
    [Display(Name ="$skip", ShortName = "$skip")]
    public int Skip { get; set; }
    [Display(Name = "$top", ShortName = "$top")]
    public int Top { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我只是使用

return Json(payload)
Run Code Online (Sandbox Code Playgroud)

但是,我的响应有效负载如下所示:

"ResponseMsg": {
    "Skip": 0,
    "Top": 3
}
Run Code Online (Sandbox Code Playgroud)

我需要它是:

"ResponseMsg": {
    "$skip": 0,
    "$top": 3
}
Run Code Online (Sandbox Code Playgroud)

解决此问题的最佳选择是什么?我需要编写自己的ContractResolver或Converter吗?

c# asp.net-core json-serialization

7
推荐指数
3
解决办法
5108
查看次数

什么是 built_value 对象的 setter

我正在尝试在flutter中使用built_value,发现如果我声明了一个Type use built_value,我通常可以使用点语法为其属性赋值:我的声明是:

abstract class Post implements Built<Post, PostBuilder> {
    Post._();
    int get userId;
    int get id;
    String get title;
    String get body;
    factory Post([updates(PostBuilder b)]) = _$Post;
    static Serializer<Post> get serializer => _$postSerializer;
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

Post p = Post();
p.titie = "hello world";
Run Code Online (Sandbox Code Playgroud)

得到错误:

[dart] 在“Post”类中​​没有名为“title”的 setter。

我不熟悉这个builder东西,即使我发现它PostBuilder具有所有属性的设置器: PostBuilder().title = 'hello world'; 但我该如何使用它?

code-generation flutter json-serialization

6
推荐指数
1
解决办法
2411
查看次数

Angular 在 POST 请求中序列化具有特定格式的日期

我是 Angular 的新手,对于序列化添加到 POST 请求的对象的 Date 属性的最佳方法是什么有几个疑问。给定样本类

export class MyClass{
    public dateProperty: Date;
}
Run Code Online (Sandbox Code Playgroud)

我在服务中有以下代码:

public addMyClass(myClass: MyClass): Observable<MyClass> {
    return this.http.post<MyClass>(this.apiBaseUrl, myClass);
}
Run Code Online (Sandbox Code Playgroud)

我必须按以下格式序列化日期'yyyy-MM-dd hh:mm'。我考虑了不同的方法,例如定义装饰器(如果可能)或重写toJson()方法,但我不知道这些是唯一的选择还是有更好的解决方案......

json date json-serialization angular angular7

6
推荐指数
1
解决办法
2408
查看次数

flutter json_serialized tojson 无法正常工作

我查看Order类示例,发现 Item 类未转换为 Map。

class Order {
  int count;
  int itemNumber;
  bool isRushed;
  Item item; 
  Map<String, dynamic> toJson() => _$OrderToJson(this);
}
Run Code Online (Sandbox Code Playgroud)

生成的 .g 文件具有以下内容:

Map<String, dynamic> _$OrderToJson(Order instance) {
  ...
  writeNotNull('item', instance.item);
  ...
  return val;
}
Run Code Online (Sandbox Code Playgroud)

订单地图中的项目仍然是项目类型,但我希望它也能自动转换为地图。生成的 .g 文件应该有这样的内容

writeNotNull('item', instance.item.toJson());
Run Code Online (Sandbox Code Playgroud)

我不想手动添加它,因为重新生成 .g 文件时它将被覆盖。为什么 json_serialized lib 没有做这么简单的事情,或者我错过了什么?谢谢。

flutter json-serialization

6
推荐指数
2
解决办法
5359
查看次数

json_serialized 插件不支持“文件”类型?

我正在使用 json_serialized 插件,但它似乎不适用于图像文件。未生成“myclass.g.dart”。我对其他类型没有任何麻烦。\n( https://pub.dev/packages/json_serialized/versions/0.5.4#-readme-tab- )

\n\n

这是我的代码:

\n\n
import \'dart:io\';\n\nimport \'package:flutter/material.dart\';\nimport \'package:json_annotation/json_annotation.dart\';\n\npart \'myclass.g.dart\';\n\n@JsonSerializable()\nclass MyClass {\n  final String name;\n  final List<File> photosFile;\n\n  MyClass({\n    @required this.name,\n    @required this.photosFile,\n  });\n\n\n  factory MyClass.fromJson(Map<String, dynamic> json) => _$MyClassFromJson(json);\n  Map<String, dynamic> toJson() => _$MyClassToJson(this);\n\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是错误:

\n\n
[SEVERE] json_serializable:json_serializable on lib/model/myclass.dart (cached):\nError running JsonSerializableGenerator\nCould not generate `fromJson` code for `photosFile` because of type `File`.\nNone of the provided `TypeHelper` instances support the defined type.\npackage:mydomus_edl/model/myclass.dart:11:20\n   \xe2\x95\xb7\n17 \xe2\x94\x82   final List<File> photosFile;\n   \xe2\x94\x82                    ^^^^^^^^^^\n   \xe2\x95\xb5\n[SEVERE] Failed after 171ms\n
Run Code Online (Sandbox Code Playgroud)\n\n

有人有主意吗?谢谢 …

dart flutter json-serialization

6
推荐指数
1
解决办法
2133
查看次数

Flutter/Dart JSON 和现有库类的序列化

我有一堂课:

import 'package:google_maps_flutter/google_maps_flutter.dart';

class Place {
  Place({
    this.address,
    this.coordinates,
  });

  final String address;
  final LatLng coordinates;
}
Run Code Online (Sandbox Code Playgroud)

LatLng是google_maps_flutter的一类。如何使用and使我的Place类可序列化?json_annotationjson_serializable

非常感谢!

serialization json dart flutter json-serialization

6
推荐指数
1
解决办法
1281
查看次数

使用 dart json_serialized 序列化时如何更改属性名称?

这是一个 json 文件 person.json

{
  "first_name": "John",
  "last_name": "Doe"
}
Run Code Online (Sandbox Code Playgroud)

这是 Person 类

import 'package:json_annotation/json_annotation.dart';

part 'person.g.dart';

@JsonSerializable()
class Person {
  /// The generated code assumes these values exist in JSON.
  final String first_name, last_name;

  /// The generated code below handles if the corresponding JSON value doesn't
  /// exist or is empty.
  final DateTime? dateOfBirth;

  Person({required this.first_name, required this.last_name, this.dateOfBirth});

  /// Connect the generated [_$PersonFromJson] function to the `fromJson`
  /// factory.
  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);

  /// Connect the …
Run Code Online (Sandbox Code Playgroud)

json dart json-serialization

6
推荐指数
1
解决办法
3416
查看次数

Python Properties 类的 Json 序列化

我有一个属性类:

from child_props import ChildProps

class ParentProps(object):
    """Contains all the attributes for CreateOrderRequest"""

    def __init__(self):
        self.__prop1 = None            
        self.__child_props = ChildProps()            

    @property
    def prop1(self):
        return self.__prop1

    @prop1.setter
    def prop1(self, value):
        self.__prop1 = value

    @property
    def child_props(self):
        return self.__child_props

    @child_props.setter
        def child_props(self, value):
        self.__child_props = value
Run Code Online (Sandbox Code Playgroud)

另一个类是:

class ChildProps(object):
    """Contains all the attributes for CreateOrderRequest"""

    def __init__(self):
        self.__child_prop1 = None        
        self.__child_prop2 = None


    @property
    def child_prop1(self):
        return self.__child_prop1

    @child_prop1.setter
    def child_prop1(self, value):
        self.__child_prop1 = value

    @property
    def child_prop2(self):
        return self.__child_prop2

    @child_prop2.setter …
Run Code Online (Sandbox Code Playgroud)

python serialization json python-2.7 json-serialization

5
推荐指数
1
解决办法
1634
查看次数

将字符串转换为类型“System.Text.Json.JsonElement”时出错

我有一个类,其中我在从 json 文件填充 jsonElement 时遇到一些问题

{
    "entities": [
        {
            "name": "DateTimeENT1",
            "description": "This a  example",
            "uil": {
                      "uill": "This is my Layout"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

正在被反序列化到此类中:

public class Container {

    public ICollection<Entity> Entities {get; set;}
}


public class Entity {
    public string Name {get; set;}
    public string Descripton {get; set;}
    UIL Uil {get; set;}
}

public class UIL{
    JsonElement Uill {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

这就是我反序列化它的方式:

var input= JsonConvert.DeserializeObject<Container>(File.ReadAllText(@"init.json"));
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到一条错误消息'Error converting value "This is my Layout" to type 'System.Text.Json.JsonElement'. 我该如何克服这个问题? …

c# json asp.net-core json-serialization

5
推荐指数
2
解决办法
1万
查看次数

如何使用 System.Text.Json 将对象属性序列化为小写?

我有一个 ASP.NET 5 MVC Core 应用程序控制器,其中包含以下代码:

using System.Text.Json;

public async Task<IActionResult> EstoAPICall() {
  ...
  EstoOst estoOst;
  var json = JsonSerializer.Serialize(estoOst);
  StringContent content = new(json, Encoding.UTF8, "application/json");
  using var response = await httpClient.PostAsync("https://example.com", content);
  ...
}

public class EstoOst {
  public decimal Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这会导致错误,因为 API 需要amountJSON 中的小写字母,但.Serialize(...)返回的是大写字母Amount

我怎样才能解决这个问题?

切换到 Json.NET,或者将类属性名称更改为小写似乎不是好的解决方案。

c# asp.net-core-mvc asp.net-core json-serialization system.text.json

5
推荐指数
1
解决办法
9129
查看次数