42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using DevDisciples.Json.Parser;
|
|
using DevDisciples.Parsing;
|
|
|
|
namespace DevDisciples.Json.Tools;
|
|
|
|
public static partial class Json2CSharpTranslator
|
|
{
|
|
public static class JsonObjectTranslator
|
|
{
|
|
public static ITranslation Translate(ISyntaxNode visitee, object[] args)
|
|
{
|
|
var context = ContextFromArgs(args);
|
|
var name = NameFromArgs(args);
|
|
var @class = new ClassTranslation { Name = name, Properties = new() };
|
|
var @object = (JsonObject)visitee;
|
|
|
|
context.Classes.Add(@class);
|
|
|
|
foreach (var prop in @object.Properties)
|
|
{
|
|
var visitor = Visitors[prop.Value.GetType()];
|
|
var translation = visitor(prop.Value, context, prop.Key);
|
|
|
|
switch (translation)
|
|
{
|
|
case ClassTranslation:
|
|
@class.Properties.Add(new PropertyTranslation
|
|
{
|
|
Type = translation.Name,
|
|
Name = translation.Name,
|
|
});
|
|
break;
|
|
case PropertyTranslation property:
|
|
@class.Properties.Add(property);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return @class;
|
|
}
|
|
}
|
|
} |