-
Notifications
You must be signed in to change notification settings - Fork 117
fix #1679: добавлен метод ОткрытьПоток() объекта ЧтениеJSON #1680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
777fac1
0297f46
0ca87f7
021d90f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,15 +5,16 @@ This Source Code Form is subject to the terms of the | |||||||||||||||||||||||||||||||||||||||
| at http://mozilla.org/MPL/2.0/. | ||||||||||||||||||||||||||||||||||||||||
| ----------------------------------------------------------*/ | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||
| using System.IO; | ||||||||||||||||||||||||||||||||||||||||
| using Newtonsoft.Json; | ||||||||||||||||||||||||||||||||||||||||
| using OneScript.Commons; | ||||||||||||||||||||||||||||||||||||||||
| using OneScript.Contexts; | ||||||||||||||||||||||||||||||||||||||||
| using OneScript.Exceptions; | ||||||||||||||||||||||||||||||||||||||||
| using OneScript.StandardLibrary.Binary; | ||||||||||||||||||||||||||||||||||||||||
| using OneScript.StandardLibrary.Text; | ||||||||||||||||||||||||||||||||||||||||
| using ScriptEngine.Machine; | ||||||||||||||||||||||||||||||||||||||||
| using ScriptEngine.Machine.Contexts; | ||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||
| using System.IO; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| namespace OneScript.StandardLibrary.Json | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -266,6 +267,38 @@ public void OpenFile(string JSONFileName, IValue encoding = null) | |||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||
| /// Устанавливает поток для чтения JSON данным объектом. | ||||||||||||||||||||||||||||||||||||||||
| /// Если перед вызовом данного метода уже производилось чтение JSON из другого файла, строки или потока, | ||||||||||||||||||||||||||||||||||||||||
| /// то чтение прекращается и объект инициализируется для чтения из указанного потока. | ||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||
| /// <param name="stream"> | ||||||||||||||||||||||||||||||||||||||||
| /// Поток для чтения текста JSON.</param> | ||||||||||||||||||||||||||||||||||||||||
| /// <param name="encoding"> | ||||||||||||||||||||||||||||||||||||||||
| /// Позволяет задать кодировку входного потока.</param> | ||||||||||||||||||||||||||||||||||||||||
| [ContextMethod("ОткрытьПоток", "OpenStream")] | ||||||||||||||||||||||||||||||||||||||||
| public void OpenStream(IValue streamContext, IValue encoding = null) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| if (IsOpen()) | ||||||||||||||||||||||||||||||||||||||||
| Close(); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| var stream = streamContext switch | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| GenericStream s => s.GetUnderlyingStream(), | ||||||||||||||||||||||||||||||||||||||||
| FileStreamContext s => s.GetUnderlyingStream(), | ||||||||||||||||||||||||||||||||||||||||
| MemoryStreamContext s => s.GetUnderlyingStream(), | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| _ => throw RuntimeException.InvalidNthArgumentType(1) | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| var enc = encoding != null ? TextEncodingEnum.GetEncoding(encoding) : System.Text.Encoding.UTF8; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| _reader = new JsonReaderInternal(new StreamReader(stream, enc)) | ||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||
| SupportMultipleContent = true | ||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+253
to
+258
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align
Proposed fix- var enc = encoding != null ? TextEncodingEnum.GetEncoding(encoding) : System.Text.Encoding.UTF8;
-
- _reader = new JsonReaderInternal(new StreamReader(stream, enc, leaveOpen:true))
- {
- SupportMultipleContent = true
- };
+ try
+ {
+ var enc = encoding != null ? TextEncodingEnum.GetEncoding(encoding) : System.Text.Encoding.UTF8;
+
+ _reader = new JsonReaderInternal(new StreamReader(stream, enc, leaveOpen: true))
+ {
+ SupportMultipleContent = true
+ };
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e.Message, e);
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||
| /// Если текущее значение – начало массива или объекта, то пропускает его содержимое и конец. | ||||||||||||||||||||||||||||||||||||||||
| /// Для остальных типов значений работает аналогично методу Прочитать(). | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а просто IStreamWrapper не пойдет? Они все его реализуют