C
C#2y ago
thieved

substringing and slicing not working..

i've tried everything on planet earth it feels like
character.description.ToString()[new Range(0, 2043)]
character.description.ToString()[new Range(0, 2043)]
character.description.ToString().Substring(0, 2043)
character.description.ToString().Substring(0, 2043)
character.description.Value.Substring(0, 2043)
character.description.Value.Substring(0, 2043)
character.description.Value.ToString(0, 2043)
character.description.Value.ToString(0, 2043)
((string)character.description).Substring(0, 2043)
((string)character.description).Substring(0, 2043)
etc... i'm converting a NewtonSoft.JValue to a string and none of these have worked, and the .Substring method exists on some of the description strings but not all of them??
19 Replies
Angius
Angius2y ago
Any reason you're using JValue (and Newtonsoft) instead of just deserializing to a proper class? Also, don't do this
str[new Range(0, 2043)]
str[new Range(0, 2043)]
it's just
str[0..2043]
str[0..2043]
thieved
thieved2y ago
im writing multiple commands, and it seems unnecessary to write classes for every result
Angius
Angius2y ago
Ah, so you're willingly getting rid of type safety and making your life harder for no reason Understandable
thieved
thieved2y ago
okay but
Angius
Angius2y ago
If writing those classes manually is such a bother, just generate them from JSON
thieved
thieved2y ago
how would i make a class for a returned http result thats an array?
Angius
Angius2y ago
Uh, deserialize to a list...? Or an array?
thieved
thieved2y ago
what json toolset do you recommend?
Angius
Angius2y ago
System.Text.Json There's no need for any 3rd party libraries Far as generating classes from JSON goes, both Rider and VS have it built-in If not, $jsongen
MODiX
MODiX2y ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Angius
Angius2y ago
There are online converters
thieved
thieved2y ago
hmm let me try hold on
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
using System.Text.Json;

var json = """
[
{ "Foo": 8 },
{ "Foo": 23 },
{ "Foo": 92 }
]
""";

record Datum(int Foo);

var data = JsonSerializer.Deserialize<Datum[]>(json);

foreach (var d in data) Console.WriteLine($"Data: {d.Foo}");
using System.Text.Json;

var json = """
[
{ "Foo": 8 },
{ "Foo": 23 },
{ "Foo": 92 }
]
""";

record Datum(int Foo);

var data = JsonSerializer.Deserialize<Datum[]>(json);

foreach (var d in data) Console.WriteLine($"Data: {d.Foo}");
Console Output
Data: 8
Data: 23
Data: 92
Data: 8
Data: 23
Data: 92
Compile: 849.384ms | Execution: 159.421ms | React with ❌ to remove this embed.
thieved
thieved2y ago
is the deserializer able to convert lowercased values to uppercased id -> Id
Angius
Angius2y ago
Values or property names?
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
using System.Text.Json;

var json = """
[
{ "foo": 8 },
{ "fOO": 23 },
{ "foO": 92 }
]
""";

record Datum(int Foo);

var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var data = JsonSerializer.Deserialize<Datum[]>(json, options );

foreach (var d in data) Console.WriteLine($"Data: {d.Foo}");
using System.Text.Json;

var json = """
[
{ "foo": 8 },
{ "fOO": 23 },
{ "foO": 92 }
]
""";

record Datum(int Foo);

var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var data = JsonSerializer.Deserialize<Datum[]>(json, options );

foreach (var d in data) Console.WriteLine($"Data: {d.Foo}");
Console Output
Data: 8
Data: 23
Data: 92
Data: 8
Data: 23
Data: 92
Compile: 786.807ms | Execution: 122.463ms | React with ❌ to remove this embed.
thieved
thieved2y ago
names oh it works let me try now
namespace Elfin.Types
{
public class AniListCharacter
{
public required int Id { get; init; }
public required AniListName Name { get; init; }
public required AniListImage Image { get; init; }
public required string Gender { get; init; }
public string? Age { get; init; }
public string? BloodType { get; init; }
public required int Favourites { get; init; }
public required string Description { get; init; }
public required string SiteUrl { get; init; }
public required AniListMedia Media { get; init; }
}

public class AniListData
{
public required AniListCharacter Character { get; set; }
}

public class AniListMedia
{
public required List<AniListMediaEdge> Edges { get; set; }
}

public class AniListMediaEdge
{
public int id { get; set; }
}

public class AniListName
{
public required string Full { get; set; }
}

public class AniListImage
{
public string? Large { get; set; }
public string? Medium { get; set; }
}
}
namespace Elfin.Types
{
public class AniListCharacter
{
public required int Id { get; init; }
public required AniListName Name { get; init; }
public required AniListImage Image { get; init; }
public required string Gender { get; init; }
public string? Age { get; init; }
public string? BloodType { get; init; }
public required int Favourites { get; init; }
public required string Description { get; init; }
public required string SiteUrl { get; init; }
public required AniListMedia Media { get; init; }
}

public class AniListData
{
public required AniListCharacter Character { get; set; }
}

public class AniListMedia
{
public required List<AniListMediaEdge> Edges { get; set; }
}

public class AniListMediaEdge
{
public int id { get; set; }
}

public class AniListName
{
public required string Full { get; set; }
}

public class AniListImage
{
public string? Large { get; set; }
public string? Medium { get; set; }
}
}
looking good so far i think
Angius
Angius2y ago
I still see one lowercase property name there, but yeah, LGTM
thieved
thieved2y ago
oh yea id
HttpResponseMessage response = await elfin.HttpClient.GetAsync("https://nekos.life/api/v2/img/neko");
string rawResponse = await response.Content.ReadAsStringAsync();
dynamic? data = JsonSerializer.Deserialize<NekosLifeResponse>(rawResponse, new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
});

await context.Message.RespondAsync(data!.Url);
HttpResponseMessage response = await elfin.HttpClient.GetAsync("https://nekos.life/api/v2/img/neko");
string rawResponse = await response.Content.ReadAsStringAsync();
dynamic? data = JsonSerializer.Deserialize<NekosLifeResponse>(rawResponse, new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
});

await context.Message.RespondAsync(data!.Url);
tried it with this
namespace Elfin.Types
{
public class NekosLifeResponse
{
public required NekosLifeData Data;
}

public class NekosLifeData
{
public required string Url;
}
}
namespace Elfin.Types
{
public class NekosLifeResponse
{
public required NekosLifeData Data;
}

public class NekosLifeData
{
public required string Url;
}
}
the returned value is {"data": {"url: "link here"}} oh i think i forgot to change the type hold on