{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "Installing package Utf8Json.....done!"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Successfully added reference to package Utf8Json, version 1.3.7"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:Utf8Json\"\n",
    "\n",
    "using Utf8Json; \n",
    "using System.Runtime.Serialization;\n",
    "using System.Collections.Generic;\n",
    "using System.Collections.ObjectModel;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"Name\":\"ddd\",\"Value\":5,\"Weight\":54.2}\n",
      "[{\"Name\":\"ddd\",\"Value\":5,\"Weight\":54.2}]\n"
     ]
    },
    {
     "ename": "Unhandled Exception",
     "evalue": "The value \"System.Collections.Generic.Dictionary`2[System.String,System.Object]\" is not of type \"Submission#14+Item\" and cannot be used in this generic collection. (Parameter 'value')",
     "output_type": "error",
     "traceback": [
      "Unhandled Exception",
      "The value \"System.Collections.Generic.Dictionary`2[System.String,System.Object]\" is not of type \"Submission#14+Item\" and cannot be used in this generic collection. (Parameter 'value')",
      "   at System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object value)",
      "   at Utf8Json.Formatters.NonGenericListFormatter`1.Deserialize(JsonReader& reader, IJsonFormatterResolver formatterResolver)",
      "   at Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, Int32 offset, IJsonFormatterResolver resolver)",
      "   at Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, IJsonFormatterResolver resolver)",
      "   at Utf8Json.JsonSerializer.Deserialize[T](String json, IJsonFormatterResolver resolver)",
      "   at Utf8Json.JsonSerializer.Deserialize[T](String json)",
      "   at Submission#14.<<Initialize>>d__0.MoveNext()",
      "--- End of stack trace from previous location where exception was thrown ---",
      "   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
     ]
    }
   ],
   "source": [
    "public class Item \n",
    "{\n",
    "    \n",
    "    public string Name {get;set;}\n",
    "    public int Value {get;set;}\n",
    "    public double Weight {get;set;}\n",
    "    [IgnoreDataMember]\n",
    "    public ItemCollection Parent {get;set;}\n",
    "}\n",
    "\n",
    "public class ItemCollection : KeyedCollection<string, Item>\n",
    "{\n",
    "    protected override string GetKeyForItem(Item item) { return item.Name; }\n",
    "}\n",
    "\n",
    "var first = new Item() {Name = \"ddd\", Value = 5, Weight = 54.2}; \n",
    "\n",
    "Console.WriteLine( JsonSerializer.ToJsonString(first)); \n",
    "\n",
    "var Col = new ItemCollection(); \n",
    "Col.Add(first);\n",
    "\n",
    "Console.WriteLine( JsonSerializer.ToJsonString(Col));\n",
    "\n",
    "var ColDes =  JsonSerializer.Deserialize<ItemCollection>(JsonSerializer.ToJsonString(Col));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}