Sky
Sky
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
Hi there! I'm struggling at making my classes to correctly be serialized. I have an interface:
public interface IComponent
{
public ComponentType ComponentType { get; }
public List<IComponent> Children { get; }
public IComponentClickEvent? ClickEvent { get; set; }
public IComponentStyle Style { get; set; }
}
public interface IComponent
{
public ComponentType ComponentType { get; }
public List<IComponent> Children { get; }
public IComponentClickEvent? ClickEvent { get; set; }
public IComponentStyle Style { get; set; }
}
And its implementation:
public class TextComponent : IComponent
{

[JsonProperty("text")]
public string Text { get; set; }
public TextComponent(string text, IComponentStyle? style)
{
if (string.IsNullOrWhiteSpace(text))
throw new System.ArgumentException("Given text cannot be null or empty", nameof(text));

Text = text;
}

[JsonProperty("type"), JsonConverter(typeof(ComponentTypeSerializer))]
public ComponentType ComponentType { get; } = ComponentType.Text;
public string Type { get; } = "text";

[JsonProperty("extra")]
public List<IComponent> Children { get; } = new();

public IComponentClickEvent? ClickEvent { get; set; }
public IComponentStyle Style { get; set; }
}
public class TextComponent : IComponent
{

[JsonProperty("text")]
public string Text { get; set; }
public TextComponent(string text, IComponentStyle? style)
{
if (string.IsNullOrWhiteSpace(text))
throw new System.ArgumentException("Given text cannot be null or empty", nameof(text));

Text = text;
}

[JsonProperty("type"), JsonConverter(typeof(ComponentTypeSerializer))]
public ComponentType ComponentType { get; } = ComponentType.Text;
public string Type { get; } = "text";

[JsonProperty("extra")]
public List<IComponent> Children { get; } = new();

public IComponentClickEvent? ClickEvent { get; set; }
public IComponentStyle Style { get; set; }
}
I'm also using this serializer:
public class ComponentTypeSerializer : JsonConverter<ComponentType>
{
public override void WriteJson(JsonWriter writer, ComponentType value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString().ToLower());
}

public override ComponentType ReadJson(JsonReader reader, Type objectType, ComponentType existingValue, bool hasExistingValue,
JsonSerializer serializer)
{
throw new NotImplementedException("Deserializing ComponentType is not supported.");
}
}
public class ComponentTypeSerializer : JsonConverter<ComponentType>
{
public override void WriteJson(JsonWriter writer, ComponentType value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString().ToLower());
}

public override ComponentType ReadJson(JsonReader reader, Type objectType, ComponentType existingValue, bool hasExistingValue,
JsonSerializer serializer)
{
throw new NotImplementedException("Deserializing ComponentType is not supported.");
}
}
And I'm serializing my value this way:
var settings = new JsonSerializerSettings
{
Converters = new JsonConverter[]
{
new Serializers.ComponentTypeSerializer(),
new Serializers.ComponentColorSerializer()
},
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
Formatting = Formatting.None
};
return JsonConvert.SerializeObject(component, settings);
var settings = new JsonSerializerSettings
{
Converters = new JsonConverter[]
{
new Serializers.ComponentTypeSerializer(),
new Serializers.ComponentColorSerializer()
},
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
Formatting = Formatting.None
};
return JsonConvert.SerializeObject(component, settings);
However, I am unable to get the type property working! It literally never shows when serializing my class. For instance, I'm getting {"text": "Hello There", "extra":[]} when serializing new TextComponent("Hello There") Can anyone help me about this? Thanks ^.^
22 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
Hi there! I'm working on a sort of addon-like system for one of my project, and would like to handle addon's dependencies using NuGet repositories. Right now, I'm downloading the DLLs requested by an addon (for instance in my test, it was SpacedGrid-Avalonia version 11.0.0) into a folder, then loading the DLLs using Assembly#Load. However, when calling this method, I'm getting a Bad IL Format, which I guess means the downloaded DLLs was not for the right machine arch. Here's my code for downloading from NuGet repos:
using var packageStream = new MemoryStream();

await resource.CopyNupkgToStreamAsync(
dependencyName, latestVersion,
packageStream, cache,
NullLogger.Instance, default);

var folder = Path.Combine(AppConfig.AppDataFolderPath, "Addons", "Dependencies");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);

var filePath = Path.Combine(folder, $"{dependencyName}_{latestVersion}.dll");
await File.WriteAllBytesAsync(filePath, packageStream.ToArray());
using var packageStream = new MemoryStream();

await resource.CopyNupkgToStreamAsync(
dependencyName, latestVersion,
packageStream, cache,
NullLogger.Instance, default);

var folder = Path.Combine(AppConfig.AppDataFolderPath, "Addons", "Dependencies");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);

var filePath = Path.Combine(folder, $"{dependencyName}_{latestVersion}.dll");
await File.WriteAllBytesAsync(filePath, packageStream.ToArray());
138 replies