IonKiwi.Json - Yet another json parser

Posted on 2019-06-12

IonKiwi.Json

Yet another json parser
Features:
  • parse/write JSON content
  • parse/write ECMAScript like content
    • Unquoted property names
    • Single / multi-line comments
    • Trailing comma allowed for objects and arrays
    • Single quoted strings
    • Multi-line strings (by escaping new line characters)
    • Unicode CodePoint escape
    • Hexadecimal/octal/binary numbers
    • Numbers with leading or trailing decimal point
    • Positive infinity, negative infinity, NaN
    • Explicit plus sign for numbers
  • Support for C#/.NET Tuples (using Tuple Element Names)
  • async writing/parsing

Usage

Parsing json
  • parsing a json string synchronously
using (var reader = new StringReader(json)) {
  var value = JsonParser.ParseSync(new JsonReader(reader));
}

or

var value = JsonUtility.ParseSync(json);
  • parsing a json string asynchronously
using (var reader = new StringReader(json)) {
  var value = await JsonParser.Parsec(new JsonReader(reader));
}

or

var value = await JsonUtility.Parse(json);
  • parsing a json stream synchronously
using (var reader = new StreamReader(stream)) {
  var value = JsonParser.ParseSync(new JsonReader(reader));
}

or

var value = JsonUtility.ParseSync(stream);
  • parsing a json stream asynchronously
using (var reader = new StreamReader(stream)) {
  var value = await JsonParser.Parse(new JsonReader(reader));
}

or

var value = await JsonUtility.Parse(stream);
Writing json
  • serializing a value as json string synchronously
var sb = new StringBuilder();
using (var writer = new StringWriter(sb)) {
  var value = JsonWriter.SerializeSync(writer, value);
}
var json = sb.ToString();

or

var json = JsonUtility.SerializeSync(value);
  • serializing a value as json string asynchronously
var sb = new StringBuilder();
using (var writer = new StringWriter(sb)) {
  var value = await JsonWriter.Serialize(writer, value);
}
var json = sb.ToString();

or

var json = await JsonUtility.Serialize(value);
  • serializing a value to a stream synchronously
using (var writer = new StreamWriter(stream)) {
  JsonWriter.SerializeSync(writer, value);
}

or

JsonUtility.SerializeSync(stream, value);
  • serializing a value to a stream asynchronously
using (var writer = new StreamWriter(stream)) {
  await JsonWriter.Serialize(writer, value);
}

or

await JsonUtility.Serialize(stream, value);

Available on NuGet

Code

Mapping json request body properties in ASP.NET Core API Controllers

Posted on 2017-10-16

FromBodyProperty

FromBodyPropertyAttribute lets you bind ASP.NET Core MVC Controller action parameters from raw request body json content properties.
[FromBody] allows you to bind a single parameter with data from the request body, with multiple values you will need to use a complex object.
[FromBodyProperty] allows you to bind multiple parameters with data from the request body:
Example controller action with multiple parameters:
public MultiplyResponse Multiply([FromBodyProperty] int x, [FromBodyProperty] int y)
The name of the action parameters are specified in the request body data:
{ "x": 2, "y": 5 }

How to use

Setup

In Startup.cs add (I)FromBodyPropertyModelBinderHelper & FromBodyPropertyJsonOptionsSetup
public void ConfigureServices(IServiceCollection services) {
	services.AddMvc();
	services.AddScoped<IFromBodyPropertyModelBinderHelper, FromBodyPropertyModelBinderHelper>();
	services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, FromBodyPropertyJsonOptionsSetup>());
}

Example controller

public class HomeController : Controller {
	public MultiplyResponse Multiply([FromBodyProperty] int x, [FromBodyProperty] int y) {
		return new MultiplyResponse() { Result = x * y };
	}
}

Example ajax request

var request = {
	x: 2,
	y: 5
}

var ajaxOptions = {
	url: "/Home/Multiply",
	dataType: "json",
	contentType: "application/json; charset=utf-8",
	type: "POST",
	data: JSON.stringify(request),
	success: function (data, textStatus, jqXHR) {
		alert(JSON.stringify(data, null, 4));
	},
	error: function (jqXHR, textStatus, errorThrown) {
		if (jqXHR.responseJSON) {
			alert(JSON.stringify(jqXHR.responseJSON, null, 4));
		}
		alert(textStatus);
	}
}
jQuery.ajax(ajaxOptions);

Demo project & code

https://github.com/IonKiwi/FromBodyProperty

Samsung Galaxy S5+ | G901F | kccat6xx

Posted on 2016-09-11

lz4 for .NET

Posted on 2015-01-01
lz4 for .NET, original C sources recompiled for the CLR (/clr:pure)
https://github.com/IonKiwi/lz4.net

Currently supports:
  • non streaming, using managed byte arrays
  • streaming, uses the LZ4 Framing Format (v1.4.1)

Example

non-streamed
  // compress data
  byte[] dataToCompress = ...
  byte[] compressedData = LZ4Helper.Compress(dataToCompress);
  // decompress data
  byte[] decompressedData = LZ4Helper.Decompress(compressedData);


streamed
  // compress data [with content checksum]
  using (LZ4Stream stream = LZ4Stream.CreateCompressor(innerStream, LZ4FrameBlockMode.Linked, LZ4FrameBlockSize.Max64KB, LZ4FrameChecksumMode.Content, -1, false)) {
    // write uncompressed data to the lz4 stream
    // the stream will compress the data and write it to the innerStream
    stream.Write(buffer, 0, buffer.Length); 
  }

  // compress data [with block and content checksum, start a new frame after 100 data blocks]
  using (LZ4Stream stream = LZ4Stream.CreateCompressor(innerStream, LZ4FrameBlockMode.Linked, LZ4FrameBlockSize.Max64KB, LZ4FrameChecksumMode.Block | LZ4FrameChecksumMode.Content, 100, false)) {
    // write uncompressed data to the lz4 stream
    // the stream will compress the data and write it to the innerStream
    stream.Write(buffer, 0, buffer.Length); 
  }

  // decompress data
  using (LZ4Stream stream = LZ4Stream.CreateDecompressor(innerStream, false)) {
    // the lz4 stream will read the compressed data from the innerStream
    // and return the uncompressed data in 'buffer'
    int bytesRead = stream.Read(buffer, 0, buffer.Length)
  }

Break on exception extension

Posted on 2013-11-19
[NOTE] Obsolete, extension removed
An extension to automatically continue execution when an exception occurs at a certain point/location.

Breaking when an exception is thrown can be very useful.
You can configure this using the well-known exception dialog:
http://msdn.microsoft.com/en-us/library/d14azbfh(v=vs.120).aspx


exception settings


In some cases though you don’t want to stop every time when an exception is thrown. For instance when ‘Enable Just My Code’ is disabled and you are using some kind of third party library that throws some generic (first chance) exception.

So this extension is for those cases: automatically resume execution when an exception is thrown at a specific location without pressing F5 or pressing continue in the exception dialog/popup.

To configure the extension:
Tools -> Options -> Break on Exception Helper

Add the specific locations to automatically continue execution by specifying:
  • The exception type (namespace + typename)
  • (example: System.ArgumentException)
  • The name of the function that throws the exception
  • (example: TestFirstChance.TestClassB.TestA)
  •  
  • Optionally:
  • The second stack frame number that needs to match
  • The second stack frame function name

Download links below.

Coming soon

Posted on 2012-03-11
Blog will be live soon!
new Blog().Launch()
© 2021 - Ewout van der Linden - IonKiwi.nl