JackWFinlay.Jsonize
3.0.0-alpha.1
The "JackWFinlay.Jsonize" package has moved to the package "Jsonize". Please see project site for more details.
dotnet add package JackWFinlay.Jsonize --version 3.0.0-alpha.1
NuGet\Install-Package JackWFinlay.Jsonize -Version 3.0.0-alpha.1
<PackageReference Include="JackWFinlay.Jsonize" Version="3.0.0-alpha.1" />
paket add JackWFinlay.Jsonize --version 3.0.0-alpha.1
#r "nuget: JackWFinlay.Jsonize, 3.0.0-alpha.1"
// Install JackWFinlay.Jsonize as a Cake Addin
#addin nuget:?package=JackWFinlay.Jsonize&version=3.0.0-alpha.1&prerelease
// Install JackWFinlay.Jsonize as a Cake Tool
#tool nuget:?package=JackWFinlay.Jsonize&version=3.0.0-alpha.1&prerelease
Jsonize
Convert HTML to JSON with this .Net Standard 2.0 package.
Version 3.*.*
Version 3.0.0 introduces breaking changes to the Jsonize project. The project has been completely rewritten to decouple, simplify, and keep up with new standards.
The project now splits the parsing and serialization into separate areas of concern,
as noted by the introduction of the IJsonizeParser
and IJsonizeSerializer
interfaces,
found in the JackWFinlay.Jsonize.Abstractions
package.
These can be implemented by anyone,
but a brand new parser has been written using AngleSharp as its HTML engine.
This is supplied as the JackWFinlay.Jsonize.Parser.AngleSharp
package.
There is also the JackWFinlay.Jsonize.Serializer.NewtonsoftJson
package,
which implements a basic serializer wrapping the Newtonsoft.Json
package with some useful functions.
Feel free to implement your own serializer -
there are loose plans to use the new System.Text.Json
serializer as a new package.
The JackWFinlay.Jsonize
package simply wraps the parser and serializer functions into one.
Jsonize no longer will grab any content from the internet for you;
you must supply the HTML as a string
to Jsonizer
class methods.
Usage
An example to get the site "https://jackfinlay.com" as a JSON string:
private static async Task<string> Testy(string q = "")
{
using (var client = new HttpClient())
{
string url = @"https://jackfinlay.com";
HttpResponseMessage response = await client.GetAsync(url);
string html = await response.Content.ReadAsStringAsync();
// The use of the parameterless constructors will use default settings.
AngleSharpJsonizeParser parser = new AngleSharpJsonizeParser();
NewtonsoftJsonJsonizeSerializer serializer = new NewtonsoftJsonJsonizeSerializer();
Jsonizer jsonizer = new Jsonize(parser, serializer);
return jsonizer.ParseToStringAsync();
}
}
Alternatively, get the response as a JsonizeNode
:
return jsonizer.ParseToJsonizeNodeAsync();
You can control the output with a JsonizeParserConfiguration
object, which is passed as a parameter to the constructor of the IJsonizeParser
of choice:
JsonizeParserConfiguration parserConfiguration = new JsonizeParserConfiguration()
{
NullValueHandling = NullValueHandling.Ignore,
EmptyTextNodeHandling = EmptyTextNodeHandling.Ignore,
TextTrimHandling = TextTrimHandling.Trim,
ClassAttributeHandling = ClassAttributeHandling.Array
}
JsonizeConfiguration jsonizeConfiguration = new JsonizeConfiguration
{
Parser = new AngleSharpJsonizeParser(parserConfiguration),
Serializer = new NewtonsoftJsonJsonizeSerializer()
};
Jsonizer jsonizer = new Jsonizer(jsonizeConfiguration);
Results are in the form:
{
"nodeType":"Node type e.g. Document, Element, or Comment",
"tag":"If node is Element this will display the tag e.g p, h1 ,div etc.",
"text":"If node is of type Text, this will display the text in that node.",
"attr":{
"name":"value",
"class": []
},
"children":[
{
"nodeType":"Node type e.g. Document, Element, or Comment",
"tag":"If node is Element this will display the tag e.g p, h1 ,div etc.",
"text":"If node is of type Text, this will display the text in that node.",
"child": []
}
]
}
Example:
The following HTML:
<!DOCTYPE html>
<html>
<head>
<title>Jsonize</title>
</head>
<body>
<div id="parent" class="parent-div">
<div id="child1" class="child-div child1">Some Text</div>
<div id="child2" class="child-div child2">Some Text</div>
<div id="child3" class="child-div child3">Some Text</div>
</div>
</body>
</html>
Becomes:
{
"nodeType": "Document",
"tag": null,
"text": null,
"attr": {},
"children": [
{
"nodeType": "DocumentType",
"tag": "html",
"text": null,
"attr": {},
"children": []
},
{
"nodeType": "Element",
"tag": "html",
"text": null,
"attr": {},
"children": [
{
"nodeType": "Element",
"tag": "head",
"text": null,
"attr": {},
"children": [
{
"nodeType": "Element",
"tag": "title",
"text": "Jsonize",
"attr": {},
"children": []
}
]
},
{
"nodeType": "Element",
"tag": "body",
"text": null,
"attr": {},
"children": [
{
"nodeType": "Element",
"tag": "div",
"text": null,
"attr": {
"id": "parent",
"class": [
"parent-div"
]
},
"children": [
{
"nodeType": "Element",
"tag": "div",
"text": "Some Text",
"attr": {
"id": "child1",
"class": [
"child-div",
"child1"
]
},
"children": []
},
{
"nodeType": "Element",
"tag": "div",
"text": "Some Text",
"attr": {
"id": "child2",
"class": [
"child-div",
"child2"
]
},
"children": []
},
{
"nodeType": "Element",
"tag": "div",
"text": "Some Text",
"attr": {
"id": "child3",
"class": [
"child-div",
"child3"
]
},
"children": []
}
]
}
]
}
]
}
]
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- JackWFinlay.Jsonize.Abstractions (>= 3.0.0-alpha.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.