Sky
Sky
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
Another question, how can I create my own ignore condition? Basically, if a list field is empty it's ignored
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
How can I achieve this?
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
{
"text": "Hello, \u00A75world!",
"type": "Text",
"style": {
"color": "#7FFFD4",
"bold": true,
"italic": false,
"underlined": true,
"strikethrough": false,
"obfuscated": false
}
}
{
"text": "Hello, \u00A75world!",
"type": "Text",
"style": {
"color": "#7FFFD4",
"bold": true,
"italic": false,
"underlined": true,
"strikethrough": false,
"obfuscated": false
}
}
And I am looking for something like:
{
"text": "Hello, \u00A75world!",
"type": "Text",

"color": "#7FFFD4",
"bold": true,
"italic": false,
"underlined": true,
"strikethrough": false,
"obfuscated": false
}
{
"text": "Hello, \u00A75world!",
"type": "Text",

"color": "#7FFFD4",
"bold": true,
"italic": false,
"underlined": true,
"strikethrough": false,
"obfuscated": false
}
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
Ah sorry, I got a last one; I have a custom serializer for a type:
public class ComponentStyleConverter : JsonConverter<ComponentStyle>
{

public override ComponentStyle? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserializing ComponentStyle is not supported.");
}

public override void Write(Utf8JsonWriter writer, ComponentStyle value, JsonSerializerOptions options)
{
writer.WriteStartObject();

if (value.Color != null)
writer.WriteString("color", value.Color.Color);

writer.WriteBoolean("bold", value.Bold);
writer.WriteBoolean("italic", value.Italic);
writer.WriteBoolean("underlined", value.Underlined);
writer.WriteBoolean("strikethrough", value.Strikethrough);
writer.WriteBoolean("obfuscated", value.Obfuscated);

if (value.Insertion != null)
writer.WriteString("insertion", value.Insertion);

writer.WriteEndObject();
}

}
public class ComponentStyleConverter : JsonConverter<ComponentStyle>
{

public override ComponentStyle? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserializing ComponentStyle is not supported.");
}

public override void Write(Utf8JsonWriter writer, ComponentStyle value, JsonSerializerOptions options)
{
writer.WriteStartObject();

if (value.Color != null)
writer.WriteString("color", value.Color.Color);

writer.WriteBoolean("bold", value.Bold);
writer.WriteBoolean("italic", value.Italic);
writer.WriteBoolean("underlined", value.Underlined);
writer.WriteBoolean("strikethrough", value.Strikethrough);
writer.WriteBoolean("obfuscated", value.Obfuscated);

if (value.Insertion != null)
writer.WriteString("insertion", value.Insertion);

writer.WriteEndObject();
}

}
And its goal is to serialize the fields in the code object, so not in another object with key style as the current code do
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
Edit: Got it working, I have to specify the implementation's class so it scans the right class' fields
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
And I'm getting {"componentType":"Text","children":[]}
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
var options = new JsonSerializerOptions
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new JsonStringEnumConverter()
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return JsonSerializer.Serialize(component, options);
var options = new JsonSerializerOptions
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new JsonStringEnumConverter()
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return JsonSerializer.Serialize(component, options);
public class TextComponent : IComponent
{

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

Text = text;
}

[JsonPropertyName("type")]
public ComponentType ComponentType { get; } = ComponentType.Text;

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

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

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

Text = text;
}

[JsonPropertyName("type")]
public ComponentType ComponentType { get; } = ComponentType.Text;

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

public IComponentClickEvent? ClickEvent { get; set; }
public IComponentStyle Style { get; set; }
}
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
Gotcha, I'm trying to convert my current code with the system lib, but I cna't get the JsonPropertyName attribute working, am I missing something? ;-;
22 replies
CC#
Created by Sky on 7/4/2024 in #help
Newtonsoft.JSON ignore a JsonProperty
Ah really? At this point ?
22 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
i mean without any generic type xD
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
I think i'll just use object in this case
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
hum got it, thanks 👍
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
There's nothing similar to ? for generic types in C#?
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
No description
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
Yup it worked like a charm! Thanks you very much 👍
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
sort of conflicts from two sources
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
ohh oki I see
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
Thanks again!
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
yup that worked btw
138 replies
CC#
Created by Sky on 5/27/2024 in #help
Choosing what architecture NuGet.Protocol Downloads
will those still be required?
138 replies