如何在编译时生成 json 文件(及其数据)的代码?

发布于 2025-01-10 09:22:35 字数 734 浏览 1 评论 0原文

想象一下,有一个包含数据的大 json 文件。对于该版本的软件,这些数据永远不会改变。这是我的文件 data.json:

{
    coordinates: [
        {x: 1, y: 2},
        {x: 2, y: 23},
        {x: 213, y: 82},
        {x: 41, y: 22},
        // ....
    ]
}

当我们想要使用该文件时,我们可以在运行时反序列化其数据。

但这有点浪费时间,因为我们会在每次应用程序启动时执行这项工作(打开文件、读取内容、反序列化......),而不是只执行一次。

是否可以使用“代码生成器”在编译时甚至之前执行此操作? “before”的意思是:你已经可以在VS中保存一个.settings文件,然后它会自动修改App.config文件。如果有人保存或构建项目后立即生成 json 文件,是否可以生成 .cs 文件?

这是我期望的结果:文件 Data.cs:

public class Data{
    public Coordinate[] Coordinates {
        get => new Coordinate[]{
            new (1,2),
            new (2,23),
            new (213,82),
            new (41,22),
        };
    }
}

Imagine, there is a large json file with data. These data will never change for that version of the software. This is my file data.json:

{
    coordinates: [
        {x: 1, y: 2},
        {x: 2, y: 23},
        {x: 213, y: 82},
        {x: 41, y: 22},
        // ....
    ]
}

When we want to use that file, we could deserialize its data during runtime.

But that's kind of wasted time because we'd do this work on every application start (opening the file, reading the content, deserializing it, ...) instead of just once.

Is it somehow possible to use a "code generator" to do this at compile time, or even before?
"before" means: You can already save a .settings file in VS and it modifies the App.config file automatically then. Would it be possible to generate a .cs file if the json file as soon as someone saves it or builds the project?

This is what I expect as a result: A file Data.cs:

public class Data{
    public Coordinate[] Coordinates {
        get => new Coordinate[]{
            new (1,2),
            new (2,23),
            new (213,82),
            new (41,22),
        };
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2025-01-17 09:22:36

有一个在线工具可以将 JSON 转换为 C#,名为 quicktype.io,您可以将其打包为 Visual Studio 代码扩展。它不仅可以将 JSON 转换为 C#,还可以将 JSON 转换为 TypeScript、Python、Go、Ruby、Java、Swift、Rust、Kotlin、C++、Flow、Objective-C、JavaScript、Elm 和 JSON Schema。

这并不完全是您所要求的,但实现了将 JSON 静态转换为代码的目标。

There is an online tools that converts JSON to C# called quicktype.io, which you can get packaged as a Visual Studio Code Extension. Not only does it convert JSON to C#, but also to TypeScript, Python, Go, Ruby, Java, Swift, Rust, Kotlin, C++, Flow, Objective-C, JavaScript, Elm, and JSON Schema.

It's not exactly what you are asking for, but achieves the objective of statically translating JSON to code.

君勿笑 2025-01-17 09:22:36

Json 文件通常保存 json 版本的数据。如果您想在编译时获得数据,则必须将其作为字符串常量保存在 .cs 文件中。有很多在线实用程序,或者很容易编写自己的实用程序将 json 转换为字符串。这就是您所需要的。但我不知道你把 json 放在哪里。也许从头开始创建文本会更容易?尤其是你的文本,基本上是一组短字符串。

但我不确定这会显着节省


//ConstCoordianates.cs
public static class ConstCoordianates
{

public static ReadOnlyCollection<Coordinate> coordinates { get; set; } = 
new ReadOnlyCollection<Coordinate>(JsonConvert.DeserializeObject<Data>
(Coordinates).Coordinates);

    public const string Coordinates = @"{
    ""coordinates"": [{
            ""x"": 1,
            ""y"": 2
        },
        {
            ""x"": 2,
            ""y"": 3
        }
    ]
    }";
}

课程时间

public partial class Data
{
    [JsonProperty("coordinates")]
    public List<Coordinate> Coordinates { get; set; }
}

public partial class Coordinate
{
    [JsonProperty("x")]
    public long X { get; set; }

    [JsonProperty("y")]
    public long Y { get; set; }
}

Json file usually keeps a json version of data. If you want to have data in a compile time you will have to keep it in .cs file as a string constant. There are lot of online utilities or it is easy to write your own to convert json to string. This is what you only need. But I can't see where do you take the json. Maybe it would be easier to create a text from the beginning? Especially your text that an array of short strings basically.

But I am not sure that will save time significantly


//ConstCoordianates.cs
public static class ConstCoordianates
{

public static ReadOnlyCollection<Coordinate> coordinates { get; set; } = 
new ReadOnlyCollection<Coordinate>(JsonConvert.DeserializeObject<Data>
(Coordinates).Coordinates);

    public const string Coordinates = @"{
    ""coordinates"": [{
            ""x"": 1,
            ""y"": 2
        },
        {
            ""x"": 2,
            ""y"": 3
        }
    ]
    }";
}

classes

public partial class Data
{
    [JsonProperty("coordinates")]
    public List<Coordinate> Coordinates { get; set; }
}

public partial class Coordinate
{
    [JsonProperty("x")]
    public long X { get; set; }

    [JsonProperty("y")]
    public long Y { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文