简体   繁体   中英

Generate XML or JSON given a C# class

Hoping that this isn't going to run foul of the "recommend or find a tool, library or favourite off-site resource" off-topic rule but here goes anyway.

If you do database development then you'll likely be familiar with tools that are out there to put sample data into your tables to allow for testing.

I'm wondering if there's any way[0] that one can take a model class marked as Serializable and generate sample XML or JSON from the properties decorated with the appropriate attributes.

There are plenty questions both on SO and elsewhere about generating C# classes from JSON or XML but what I want is slightly different.

The specific use case here is that I'm waiting on JSON output from another developer and wondering if there's a way I can get a jumpstart so I can test my code, using this method, whatever this method may be.

I'm just using the standard XML serialiser and JSON.NET libraries but can switch to something else if needed.

[0] I know I can write code that can instantiate a class using Reflection, then reflect into that instance and write XML or JSON that way, but that's a big project in and of itself. I'm hoping for a pre-existing way in the .NET Framework or some kind of a FOSS tool

You could use some of the unit testing tooling out there to help you out.

For example nBuilder could populate your properties with sample data and then serialize it to json.

void Main()
{
    var samplePerson = Builder<Person>.CreateNew().Build();

    var json = JsonConvert.SerializeObject(samplePerson);

    //outputs {"Name":"Name1","Age":1} to the screen
    json.Dump();
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Here's the linqpad code: http://share.linqpad.net/prur3p.linq

This however notably won't be using your property attributes for aid.

A more detailed example:

var hierarchySpec = Builder<HierarchySpec<Category>>.CreateNew()
                .With(x => x.AddMethod = (parent, child) => parent.AddChild(child))
                .With(x => x.Depth = 5)
                .With(x => x.MaximumChildren = 10)
                .With(x => x.MinimumChildren = 5)
                .With(x => x.NamingMethod = (cat, title) => cat.Title = "Category " + title)
                .With(x => x.NumberOfRoots = 10).Build();

            Builder<Category>.CreateListOfSize(2500).BuildHierarchy(hierarchySpec);

( https://github.com/garethdown44/nbuilder/#hierarchy-generation )


As a side note:

Unless you are doing "end 2 end" testing... Your question does make me think that perhaps you have a hard coded dependency on another component. You should be able to test your code without needing another component to be ready (especially if from another developer). If you're not already familiar with it I'd recommend reading martin fowler's discussion about mocks to mock the behavior of dependent components. If you are simply doing end to end testing and know all about unit testing / mocking etc, forget this paragraph. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM