Cross-Platform Binary และ JSON serializer สำหรับ. NET Dotnet
ส่วนหนึ่งของฐานข้อมูล dbreeze ที่ใช้ใน raft.net
โทรบรรทัดถัดไปเพื่อสร้างรหัสสำหรับ serialzer:
var resbof = BiserObjectify . Generator . Run ( typeof ( TS6 ) , true ,
@"D:Temp1" , forBiserBinary : true , forBiserJson : true , null ) ;อาร์กิวเมนต์แรกคือประเภทของวัตถุรากที่จะเป็นอนุกรม (สามารถมีวัตถุอื่น ๆ ที่ต้องเป็นอนุกรมด้วย) อาร์กิวเมนต์ที่สองหมายความว่า biserobjectify ต้องเตรียม serializer สำหรับวัตถุทั้งหมดที่รวมอยู่ในวัตถุราก อาร์กิวเมนต์ที่สามชี้ไปที่โฟลเดอร์ที่จะสร้างไฟล์ C# ต่อวัตถุ ข้อโต้แย้งที่สี่และห้าหมายความว่าเราต้องการใช้ทั้ง serializers ไบนารีและ JSON อาร์กิวเมนต์ที่หกคือ Hashset (หรือ Null) ที่มีชื่อคุณสมบัติที่จะไม่เป็นอนุกรม
ตัวแปร resbof จะมีข้อมูลเดียวกับที่ในไฟล์ที่สร้างขึ้นเป็นพจนานุกรม
public partial class TS6
{
public string P1 { get ; set ; }
.. . TS6 t6 = new TS6 ( )
{
P1 = "dsfs" ,
P2 = 456 ,
P3 = DateTime . UtcNow ,
P4 = new List < Dictionary < DateTime , Tuple < int , string > >>
{
new Dictionary < DateTime , Tuple < int , string > > {
{ DateTime . UtcNow . AddMinutes ( - 1 ) , new Tuple < int , string > ( 12 , "testvar" ) } ,
{ DateTime . UtcNow . AddMinutes ( - 2 ) , new Tuple < int , string > ( 125 , "testvar123" ) }
} ,
new Dictionary < DateTime , Tuple < int , string > > {
{ DateTime . UtcNow . AddMinutes ( - 3 ) , new Tuple < int , string > ( 17 , "dsfsdtestvar" ) } ,
{ DateTime . UtcNow . AddMinutes ( - 4 ) , new Tuple < int , string > ( 15625 , "sdfsdtestvar" ) }
}
} ,
P5 = new Dictionary < int , Tuple < int , string > > {
{ 12 , new Tuple < int , string > ( 478 , "dsffdf" ) } ,
{ 178 , new Tuple < int , string > ( 5687 , "sdfsd" ) }
} ,
P6 = new Tuple < int , string , Tuple < List < string > , DateTime > > ( 445 , "dsfdfgfgfg" ,
new Tuple < List < string > , DateTime > ( new List < string > { "a1" , "a2" } , DateTime . Now . AddDays ( 58 ) ) ) ,
P7 = new List < string > { "fgdfgrdfg" , "dfgfdgdfg" } ,
P8 = new Dictionary < int , List < string > > {
{ 34 , new List < string > { "drtttz" , "ghhtht" } } ,
{ 4534 , new List < string > { "dfgfghfgz" , "6546ghhtht" } }
} ,
P25 = new Dictionary < int , List < string [ , ] [ ] [ , , ] > > [ , , , ] [ ] [ , , ]
.. .
} var serializedObjectAsByteArray = t6 . BiserEncoder ( ) . Encode ( ) ;
var retoredBinaryObject = TS6 . BiserDecode ( serializedObjectAsByteArray ) ; var jsonSettings = new Biser . JsonSettings { DateFormat = Biser . JsonSettings . DateTimeStyle . ISO } ;
string prettifiedJsonString = new Biser . JsonEncoder ( t6 , jsonSettings )
. GetJSON ( Biser . JsonSettings . JsonStringStyle . Prettify ) ;
var restoredJsonObject = TS6 . BiserJsonDecode ( prettifiedJsonString , settings : jsonSettings ) ; สำหรับความเข้าใจที่ลึกซึ้ง:
เอกสาร Binary Biser
เอกสาร json biser
ตัวอย่างของการเข้ารหัส/ถอดรหัสด้วยตนเอง