You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When reading a list of structs from a text file/string, such as [{number:0},{number:1},{number:2}], the following exception is thrown at the end of the list: System.FormatException: Illegal character: expected '}' character but encountered ']'
usingSystem;usingSystem.IO;usingAmazon.IonDotnet;usingAmazon.IonDotnet.Builders;namespaceIonScratchpad{classProgram{privateconststringTextFileName="structList.txt.ion";privateconststringBinaryFileName="structList.bin.ion";staticvoidMain(string[]args){// Write BinaryWriteBinaryFile();// Read BinaryReadFile(BinaryFileName);// Write TextWriteTextFile();// Read TextReadFile(TextFileName);}privatestaticvoidReadFile(stringfileName){try{Console.WriteLine($"Reading {fileName} with {nameof(IonReaderBuilder)}");usingStreamfs=File.OpenRead(fileName);usingIIonReaderreader=IonReaderBuilder.Build(fs);ReadListOfStruct(reader);Console.WriteLine("Done reading binary file");}catch(Exceptione){Console.WriteLine($"Hit exception while reading {fileName}: {e.Message}");}}privatestaticvoidReadListOfStruct(IIonReaderreader){reader.MoveNext();// position the reader at the first value, a structreader.StepIn();// step into the listwhile(reader.MoveNext()!=IonType.None){reader.StepIn();// step into the structreader.MoveNext();// position the reader at the first value in the structstringfieldName=reader.CurrentFieldName;// retrieve the current value's field nameintvalue=reader.IntValue();// retrieve the current value's string valuereader.StepOut();// step out of the structConsole.WriteLine(fieldName+": "+value);}reader.StepOut();// step out of the list}privatestaticvoidWriteBinaryFile(){try{Console.WriteLine($"Writing {BinaryFileName} with {nameof(IonBinaryWriterBuilder)}");usingStreamfs=File.Create(BinaryFileName);usingIIonWriterbinaryWriter=IonBinaryWriterBuilder.Build(fs);WriteListOfStruct(binaryWriter);Console.WriteLine("Done writing binary file");}catch(Exceptione){Console.WriteLine($"Hit exception while writing {BinaryFileName}: {e.Message}");}}privatestaticvoidWriteTextFile(){try{Console.WriteLine($"Writing {TextFileName} with {nameof(IonTextWriterBuilder)}");usingTextWritertw=newStreamWriter(TextFileName);usingIIonWritertextWriter=IonTextWriterBuilder.Build(tw);WriteListOfStruct(textWriter);Console.WriteLine("Done writing text file");}catch(Exceptione){Console.WriteLine($"Hit exception while writing {TextFileName}: {e.Message}");}}privatestaticvoidWriteListOfStruct(IIonWriterwriter){writer.StepIn(IonType.List);for(inti=0;i<3;i++){writer.StepIn(IonType.Struct);// step into a structwriter.SetFieldName("number");// set the field name for the next value to be writtenwriter.WriteInt(i);// write the next valuewriter.StepOut();// step out of the struct}writer.StepOut();writer.Finish();}}}
Output of this stub is as follows:
Writing structList.bin.ion with IonBinaryWriterBuilder
Done writing binary file
Reading structList.bin.ion with IonReaderBuilder
number: 0
number: 1
number: 2
Done reading binary file
Writing structList.txt.ion with IonTextWriterBuilder
Done writing text file
Reading structList.txt.ion with IonReaderBuilder
number: 0
number: 1
number: 2
Hit exception while reading structList.txt.ion: Illegal character: expected '}' character but encountered ']'
And lastly the files generated by the stub are in this zip file: filesMadeByStub.zip
The text was updated successfully, but these errors were encountered:
This doesn't help explain why the text vs binary versions have different behavior but, as a workaround, after nt value = reader.IntValue(); you can call reader.MoveNext() (which returns IonType.None) before stepping out of the struct.
When reading a list of structs from a text file/string, such as
[{number:0},{number:1},{number:2}]
, the following exception is thrown at the end of the list:System.FormatException: Illegal character: expected '}' character but encountered ']'
Below is a stub program which writes and then reads a list of simple structs in both a text and binary file formats, adapted from the cookbook's reading and writing ion data section.
Output of this stub is as follows:
And lastly the files generated by the stub are in this zip file: filesMadeByStub.zip
The text was updated successfully, but these errors were encountered: