Malibloo
Malibloo
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Haven't gone through it all the way, nor have I played with it yet, my free time this week has been limited. It does seem like it's what I need, but I've had that thought a few times now, hah.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Also, for anyone who spent time on this so far, thank you. I really super appreciate it.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Intentional in the way that passing along an ID will sometimes not be useful, as a new entry will automatically gain the ID, whereas in other times where I need to update/put, I will need the ID. Currently it's ignored it if it's null. As for the Filestream, it's technically required, but I forgot about it.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Source generator is a new term to me. I'mma look it up.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
With source generator you mean making my own json converter?
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Another solution I wish I could use is to just append the json text to every object, which was the solution I used before figuring out that the API could hold multiple objects.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
It's something worth pursuing if there's really no better alternative. I'm just really stuck on the idea that just adding some text to a json structure doesn't have to be this problematic.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Didn't need to profile it, I just ran it, it took probably 20 times longer. Though I admit the code was shambly at the time, I imagine if I created some template with reflection once, instead of each time, it'd probably be faster.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
As for the example, let's see...
internal class Subject : IConnectorModel
{
[JsonPropertyName("SbId")]
public int DossierId { get; set; }
[JsonPropertyName("StId")]
public int DossierType { get; set; }
[JsonPropertyName("Ds")]
public string Subject { get; set; } = string.Empty;
[JsonPropertyName("SbTx")]
public string SubjectText { get; set; } = string.Empty;
[JsonPropertyName("Da")]
public DateTime ReceievedDate { get; set; }
[JsonPropertyName("Objects")]
public ICollection<IConnectorModel>? Objects { get; set; }
}
internal class SubjectAttachment : IConnectorModel
{
public string FileName { get; set; } = string.Empty;
public string FileId { get; set; } = string.Empty;
public byte[] FileStream { get; set; } = Array.Empty<byte>();
}
internal class Subject : IConnectorModel
{
[JsonPropertyName("SbId")]
public int DossierId { get; set; }
[JsonPropertyName("StId")]
public int DossierType { get; set; }
[JsonPropertyName("Ds")]
public string Subject { get; set; } = string.Empty;
[JsonPropertyName("SbTx")]
public string SubjectText { get; set; } = string.Empty;
[JsonPropertyName("Da")]
public DateTime ReceievedDate { get; set; }
[JsonPropertyName("Objects")]
public ICollection<IConnectorModel>? Objects { get; set; }
}
internal class SubjectAttachment : IConnectorModel
{
public string FileName { get; set; } = string.Empty;
public string FileId { get; set; } = string.Empty;
public byte[] FileStream { get; set; } = Array.Empty<byte>();
}
These are more or less the models. The JsonPropertyName is currently there for importing sake. It turns into something like:
{
"Subject": {
"Element": {
"Fields": {
"StId": "Value",
"Ds": "SubjectValue",
"Da": "SomeDate",
"SbTx": "SomeContent"
}
"Objects": [
{
"SubjectAttachment": {
"Element": {
"Fields": {
"FileName": "Name",
"FileId": "123"
} } } }
]
} } }
{
"Subject": {
"Element": {
"Fields": {
"StId": "Value",
"Ds": "SubjectValue",
"Da": "SomeDate",
"SbTx": "SomeContent"
}
"Objects": [
{
"SubjectAttachment": {
"Element": {
"Fields": {
"FileName": "Name",
"FileId": "123"
} } } }
]
} } }
As you can see, the "Element" and "Fields" is added to every object. Also it uses its own class name as well, which makes using generic, inheritance and dynamic a bit harder. And also if there's any other objects attached it's in its own "Objects" array outside of "Fields". I don't know what the person who made the API this way was thinking, but I doubt they made it in C#. I hope this gives some insight? I'm sure I'll be missing something else, as I had thought the initial post had enough information.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Can't say I've looked into dynamic before for this, mostly because one shouldn't really use dynamic. But yes, it is indeed an overly complex scenario. The simplest way I can explain is that every object needs additional json data that encapsulates it, and that every object can contain more objects.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Wow it'd be great if Discord actually sends me a notification. The problem is that the model structure of the rest API is dumb (check first post for example), and I'll have many different models that need to all go through said API, and as such a need for generic methods, which don't play well with serialization. Creating the complex hierarchy would mean needing to code for said complex hierarchy every time, which is extremely mistake prone and just annoying. Best fitting solution I got uses reflection, but is slow as heck, which is annoying when you need to go through tens of thousands of entries. So I'm kind of wondering if there's a better solution out there.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Well, I'm back here again with the same issue. The idea I had before by just converting everything to a string manually is already biting me in the ass, and now that I'm already in double digits of models, this is becoming unwieldly. The problem remains as it's in the first post. I have a model that can hold other models as collections and the structure of everything is always appended with the Element and Fields nodes. My latest attempt was creating an interface that'd use the JsonConverter to turn the object into a json string and append the necessary nodes afterwards, which at first didn't work with inheritance, but after casting it to object it did. However, the problem remained that the collections also needed the same nodes, which they didn't. This gave me the idea to create a custom converter, so it'd at the very least append the nodes, but after spending well over 4 hours trying with the help of this page https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-6-0 and not understanding what I was doing, or getting anywhere in general, I gave up on that. The last idea I have is likely similar to how the Json Converter works in the first place, which is using reflection to get the field names and values when not null, and to use a stringbuilder to paste it all together, just so I can add those extra nodes. But I know for a fact that once you start doing things in reflection your program is going to slow down a lot, and what I'm creating this for will have input around the tens of thousands. Again, I can not create the structure as it is shown in the models, because this would complicate the process by forcing myself to create those worthless objects myself every single time I need any of them in any situation. I also don't see a way around this. Is there anyone with a new and/or better idea I can approach?
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
I was honestly hoping there was a much easier way to deal with custom json structures in the newish Text.Json, but it's been made clear that i have to get creative with both the models and customizing the serializer. I definitely have more ideas thanks to you peeps though, much appreciated!
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Anyway, from the responses I'm gathering I think I can safely say that there's no real proper way to deal with this. Considering that I'm fairly sure that the rest endpoint is also handled in .Net, I'd kill to see how they're actually handling it.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Hmm, I figured the example was big enough. It shows that every class has a main of Element, a key outside of fields for some reason, then fields with the actual class properties, and also Objects, which I can only imagine is used as a dynamic object array, though I'll have to double check if one can have multiple attachments. Best idea I can come up with a base class that'll have a key plus properties, but the key needs a different name for each class, so I need to somehow override the JsonProperty (is that even possible?), Then serialize all the fields that aren't null, and just use string magic to paste the element/field/objects thing. It's still a miserable solution, but I imagine it could work for anything newly introduced? As for documentation, there's none, just a page where you can manually enter the existing fields which will allow you to send the data, which I can intercept to get the json. If necessary I can try to get all the fields I think are necessary.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
As a sidenote, the Element type seems to be reused amongst the different Subject parts, which would clearly have different fields, nor would they be able to contain themselves. I can see a solution that'd end up with creating SubjectElement classes and whatnot, but that'd make it even more complex and I'm trying to find a simpler way.
49 replies
CC#
Created by Malibloo on 12/30/2022 in #help
❔ JSON Serialization Weird Model
Pardon, I had gotten completely sidetracked and missed this reply, thanks for the response. The problem with this approach is that implementing the actual data will be a nightmare of continuously creating and assigning a mass amount of objects to fill out the hierarchy each time. This won't make coding it any simpler, sadly.
49 replies
CC#
Created by .a on 12/30/2022 in #help
❔ My code cant write to a certain path
I'm not sure what the rules are here about spoonfeeding, but I'm not going to write your code for you, sorry. What you need was explained in my second and fourth message.
18 replies
CC#
Created by .a on 12/30/2022 in #help
❔ My code cant write to a certain path
Right. So you get that filename by using Path.GetFileName(), and then past that onto the end of your location. WriteAllBytes need a full path plus the filename. It doesn't just guess what the file is named.
18 replies
CC#
Created by .a on 12/30/2022 in #help
❔ My code cant write to a certain path
Is it the file name or the folder? You just said it's both.
18 replies