Skip to content

Instantly share code, notes, and snippets.

View alansingfield's full-sized avatar

Alan Singfield alansingfield

View GitHub Profile
@LexVocoder
LexVocoder / json-helpers.ts
Last active April 13, 2022 02:06
Helpers for converting JSON to actual TypeScript classes
/***
Inspired by http://choly.ca/post/typescript-json/ , except this is only for parsing (not stringifying).
If you use this, I'd appreciate some credit and some feedback ... but neither are required.
Copyright license (Apache) is at the bottom.
NOTE THIS WILL PROBABLY NOT WORK if your uglifier changes constructor/class names. This is a pretty common thing, and it makes me sad.
The main problem this solves is that JSON.stringify returns plain old JS objects, not TS instances.
namespace System.Collections.ObjectModel
{
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
/*
Everything with no dependencies comes first. That way, when a new item is added with a dependency on an existing item, it doesn't cause the existing item to jump.
Of course an existing item taking a dependency on an existing item will cause a jump, may as well move the changing item.
An existing item taking a dependency on a new item will also jump. It doesn't really make sense to putting the new item first, out of order, if it doesn't have dependencies.
Basically: order by depth_of_deepest_dependency, original_order
*/
with
creating_objects as (