How to return data in JSON format using FastAPI?

The wrong approach If you serialise the object before returning it, using json.dumps() (as shown in your example), for instance: import json @app.get(‘/user’) async def get_user(): return json.dumps(some_dict, indent=4, default=str) the JSON object that is returned will end up being serialised twice, as FastAPI will automatically serialise the return value behind the scenes. Hence, the … Read more

Serializing Lists of Classes to XML

Just to check, have you marked Bar as [Serializable]? Also, you need a parameter-less ctor on Bar, to deserialize Hmm, I used: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Foo f = new Foo(); f.BarList = new List<Bar>(); f.BarList.Add(new Bar { Property1 = … Read more

Serializing anonymous delegates in C#

Did you see this post that I wrote as a followup to the CountingDemo: http://dotnet.agilekiwi.com/blog/2007/12/update-on-persistent-iterators.html ? Unfortunately, Microsoft have confirmed that they probably will change the compiler details (one day), in a way that is likely to cause problems. (e.g. f/when you update to the new compiler, you won’t be able to deserialise the stuff … Read more

Can JavaScriptSerializer exclude properties with null/default values?

FYI, if you’d like to go with the easier solution, here’s what I used to accomplish this using a JavaScriptConverter implementation with the JavaScriptSerializer: private class NullPropertiesConverter: JavaScriptConverter { public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { var jsonExample … Read more

How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?

I finally figured out a solution to this. It’s not what I would have preferred (which would be to return the specific object type, and somehow instruct WCF to use a Json.Net serializer, instead of the DataContractJsonSerializer), but it is working great, and it’s simple and clear. Extending my contrived example using this new solution: … Read more

How does Java’s serialization work and when it should be used instead of some other persistence technique?

I would personally try to avoid Java’s “built-in” serialization: It’s not portable to other platforms It’s not hugely efficient It’s fragile – getting it to cope with multiple versions of a class is somewhat tricky. Even changing compilers can break serialization unless you’re careful. For details of what the actual bytes mean, see the Java … Read more

Creating nested dataclass objects in Python

You can use post_init for this from dataclasses import dataclass @dataclass class One: f_one: int f_two: str @dataclass class Two: f_three: str f_four: One def __post_init__(self): self.f_four = One(**self.f_four) data = {‘f_three’: ‘three’, ‘f_four’: {‘f_one’: 1, ‘f_two’: ‘two’}} print(Two(**data)) # Two(f_three=”three”, f_four=One(f_one=1, f_two=’two’))