✅ Parse .md like .json in C# class
Does anyone know how I can parse a "settings markdown" file into a C# class, like I could do with newtonsoft for JSON files?
Example:
I have this "settings.md" file
How can I parse this into a C# class like this:
(and following the same structure any number of other objects)
I'm using DecapCMS in my Blazor project, but DecapCMS creates markdown files that I want to render the content into my blazor project
The "settings" are also created in markdown, and regular content is written under the "header" from the markdown (blog example)
I'd want to parse that into a C# class too 😅 (and the regular content I can parse into HTML without issue)
71 Replies
This is what I came up wit now, and it feel clunky 😅
That's what I've been trying so far, to turn the
.md
file into a yaml file.
But technically I should be creating .md
file into yaml AND a content string. Or I have to turn it into JSON and parse it that way. But I don't know how to turn .md
into JSON objects 😅 DecapCMS does not give me .json
files sadlyWell it should be relatively easy to parse that settings file with a custom parser
If the files strictly follow the example formatting, then you can get away with even not using RegEx and relying on Spans and simple string character checks
Question is whether you wish to create a generic parser or a concrete one for the
Settings
and Header
, etc. classes you've definedI would like to create a generic parser
However, I've noticed that I'll have to parse the file into a string first instead of using
File.ReadAllLines()
because of the Blazor WASM framework, a Directory
or File.
are not available, and will result in errorsASP.NET Core Blazor file uploads
Learn how to upload files in Blazor with the InputFile component.
That's on uploaded from blazor itself. I"m looking for what DecapCMS has already uploaded and re-built using the git-pipeline. But it might give me some insight. However, I cannot seem to access a file directly, so I am currently bound to go to the URL (
localhost:7052/uploads/fpsettings/settings.md
for example) and read that as string.
Given that I cannot seem to access File
or Directory
methods within a Blazor project, and these are not "Uploaded" files by the SWA itselfI see
Where exactly are the .md files located
in your project
wwwroot/uploads
So, does this happen at runtime, everytime someone visits your page, it reads the file/-s and creates all the necessary objects to render your thing?
what I've been trying in my
index.razor
This breaks because the File.ReadAllLines()
doesn't know where /uploads/fpsettings/settings.md
is located, since the BlazorProject returns Diretory.GetCurrentDirectory() => returns "/"
So I tried to do:
However this returns an error in console:
This is what I want it do do after parsing the .md
filesYou will have to make an http request to the endpoint
Oh read as stream?
Can I from there actually parse it?
hang on tho, lemme see if this streamreader first
StreamReader doesnt read the entire file into memory
It reads line by line
That's the memory efficient and performance critical approach to parsing text
Fair enough
How big are your files circa?
in kb's?
I don't know, but I think they'll rarely exceed 2mb if they even reach that high.
They're blogs with references to uploaded images, or they are settings for various components
Do I use
streamReader.ReadToEndAsync()
or should I pass the entire stream to the Parse method I'm writing?That's up to you 😆
I think your parser benefits from this more if you give it the whole stream
Fair enough
How would you handle my parser different if it were you tho?
What is this thing called within decap
The .md file output thingy
what do you mean?
Looking at https://decapcms.org/docs/intro/ rn
Trynna find this .md output that it creates, which you mentioned
Overview | Decap CMS | Open-Source Content Management System
Open source content management for your Git workflow
DecapCMS creates markdown files
I have created a yaml file with the settings and various collections you might be able to edit.
After which it creates a markdown file in the specified folders..
I'm not sure what you mean with the "output thingy"
the file I'm working on rn is called
settings.md
but later there might be [employee-name].md
and [blog-title].md
files tooAh, so you yourself write the .md files
yes, by a few "forms" that DecapCMS creates in the
/admin/
routeAdd to Your Site | Decap CMS | Open-Source Content Management System
Open source content management for your Git workflow
yes those markdown files (in example)
that is waht I want to parse into a C# class so my components can use the data in them
My quick write up
This made me realize, it's inevitable to load all contents of the file into memory, in order to work with it <:DFrido_HaHa:845975447971692635>
But anywho, a streamreader prolly more performant
I guess? 😅
l
Imma write this down, and see if I understand what's happning gimme a sec
Shouldn't this change?
?
Ah, yes
Good catch
Though it's a method, so add
()
Yea, true sorry 😅 misspell
Also why dump the
string yml = serializer.Serialize(yamlPart);
?
Oh, wait
now I see, you're getting only the bdy
body*
Is what I have now?
just to make sure I can us this method for both a settings only
file, as well as a settings with body
fileI mean
I am not sure how much of a performance benefit turning the first two ReadLines into async has, as the whole Task-jazz has an overhead... For what? Reading and dumping 3 - 4 chars / bytes
The body can prolly get extensive, there I get it
Fair, I just get the "this has an async overload" message, and press alt+enter 😅
I cant really estimate the benefit / overhead here
this is my index.razor now 😅
Wait, why are we serializing a string to string, just to deserialize it again
what a mess lol
that's what you wrote, I'm still in process of understanding lol
Thats what you wrote initially! I was building off your stuff
Ah, fair
Looks good to me, only that you could use more vars, and less line
await base.OnInitializedAsync();
not necessary
Oh, you made the parsing an extension method <:DFrido_panik:875369065852571678>yes is 😅 there is a login component that is handled with Graph in the
base.OnInitialized()
lol yeaa
I prefer static methods that way, dunno why
change:
Looks good to me
I'm running it lol
I have
<base href="/"/>
in my index.html
guess I have to make it https://localhost:7052/uploads/fpsettings/settings.md
also that... eventhough I have a property public string SubTitle {get;set;}
in there..Case sensitivity?
I have the PascalCaseNamingConvention tho
oh wait, thats json
Damn google, I specified yaml!
🤣
Maybe this doesn't work like the JSON parser, I've got the classes nested?
(imagine
/// <summary> </summary>
comments above each property)It should handle that
the markdown in question (example markdown)
that's actually all lol
but it will increase in size a little when I do the genuine settings 😅
the idea is to use the parsed settings like this:
<HeroBanner Title="@_settings.HeroSettings.Title" Description="@_settings.HeroSettings.SubTitle" ButtonContent="@_settings.HeroSettings.ButtonText"/>
From either the index, or whichever page/component needs it (prolly by keeping components stupid, and calling back to parents)Yea
Do I make the class property lowercase all?
nooooooooooooooooooo
that disgustaang
Yea figured lol
I need to figure a way to tell the deserializer that the parsed yaml is in underscore, and the target is in PascalCase 😅
THis is prolly why I serialized it first lol...
I dont know yaml de-/serialization all that much, and I am glad about that
So uh, you got this!
XD
I hope you made a mistake there, and that's not the actual output
<:DFrido_Hmm:704598915080781855>
how so?
Hrm, that is the acutal output... that should all have
-
in front of them...A leading dash makes it an array, without a leading dash, its a property
dies
I mean, this is the configuration file that states how they should be written
why?
or wait, it might be valid yaml
yeah, this is where my yaml knowledge ends
lol okay 😅
but I think I found what can help tho
imma test it, but yea
that will very likely work, though I was hoping for a better alternative
Guess not
dafuq...
Alias =>
So that should be right, innit?
https://github.com/aaubry/YamlDotNet/wiki/Samples.DeserializeObjectGraph
I'm going to take a break, but I really don't get this 😅Anything other than json is
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Since I'm going to change this up to parse
.md
files into .json
files. I will close this post