1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
Imports System Imports System.Collections Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Diagnostics Imports System.IO Imports System.Threading Imports System.Collections.ObjectModel Imports System.Globalization Imports System.Net Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Imports Newtonsoft.Json.Converters Imports Newtonsoft.Json.Serialization Imports System.Runtime.CompilerServices Imports System.Net.Http Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Debug.WriteLine("Json.NET version: " & GetType(JsonSerializer).Assembly.FullName) GetFileListNames(APIKeyTextBox.Text) GetFineTuneNames(APIKeyTextBox.Text) 'TestClass.Test() End Sub Private Sub ExecuteCurl() Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("https://meineURI.net") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) Dim myData As String = "yourDataHere" myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd End Sub Private Function RunCurl() Dim oProcess As New Process() Dim oStartInfo As New ProcessStartInfo("curl", "https://api.openai.com/v1/files -H ""Authorization: Bearer " & APIKeyTextBox.Text & """ -F purpose=""answers"" -F file=""@" & My.Application.Info.DirectoryPath & "\" & "myfile.jsonl""") Debug.WriteLine("curl " & oStartInfo.Arguments) oStartInfo.UseShellExecute = False oStartInfo.RedirectStandardOutput = True oProcess.StartInfo = oStartInfo oProcess.Start() Dim sOutput As String Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput sOutput = oStreamReader.ReadToEnd() End Using Console.WriteLine(sOutput) 'Add this next to standard output redirect oStartInfo.RedirectStandardError = True Button4_Click(Nothing, Nothing) Return Nothing 'Add this below Using oStreamReader As System.IO.StreamReader = oProcess.StandardError sOutput = oStreamReader.ReadToEnd() End Using End Function Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim FileToWrite As New StreamWriter(My.Application.Info.DirectoryPath & "\" & "myfile.jsonl") Dim Data As String For Each MyFile As String In IO.Directory.GetFiles("C:\Users\user\Desktop\OpenAI Dataset", "*.txt") Debug.WriteLine(MyFile) Data = IO.File.ReadAllText(MyFile) Dim Output As String Output = "" If Data.Contains("Transcribed by https://otter.ai") Then For Each MyData In Data.Split({vbCrLf & vbCrLf}, StringSplitOptions.RemoveEmptyEntries) Output &= AddToDictionaryPromptCompletation(MyData, MyFile) ' AddToDictionaryTextMeta(MyData, MyFile) Next Else Output = AddToDictionaryPromptCompletation(Data, MyFile) 'AddToDictionaryTextMeta(Data, MyFile) End If Debug.Write(Output) FileToWrite.Write(Output) Next FileToWrite.Close() Process.Start(My.Application.Info.DirectoryPath) 'AddToDictionary("Tester", "Metadata") End Sub Private Function AddToDictionaryTextMeta(ByVal myText As String, myMetadata As String) As String Return TestClass.Test(Enumerable.Range(1, 1).[Select](Function(i) JObject.FromObject(New With {Key .text = myText, Key .metadata = myMetadata}))) End Function Private Function AddToDictionaryPromptCompletation(ByVal myText As String, myMetadata As String) As String Return TestClass.Test(Enumerable.Range(1, 1).[Select](Function(i) JObject.FromObject(New With {Key .prompt = "", Key .completion = myText}))) End Function Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click If Not ListBox1.SelectedItem Is Nothing Then Dim Tempature As Decimal = CDec(NumericUpDown2.Value) / 100D Dim Response = AskAboutFile(APIKeyTextBox.Text, TextBox1.Text, ListBox1.SelectedItem.ToString.Split(" - ")(0), NumericUpDown1.Value, Tempature) Dim result = JsonConvert.DeserializeObject(Response) TextBox2.Text = result("answers")(0).ToString.Replace("/n", vbCrLf) End If End Sub Private Function CompleteUsingFile(ByVal APIKey As String, ByVal modelid As String, ByVal prompt As String, ByVal Filename As String, ByVal Number As Integer, ByVal Tempature As Decimal) Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse 'myReq = HttpWebRequest.Create("https://api.openai.com/v1/engines/text-davinci-001/completions") 'myReq = HttpWebRequest.Create("https://api.openai.com/v1/engines/" & modelid & "/completions") myReq = HttpWebRequest.Create("https://api.openai.com/v1/completions") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Bearer " & APIKey) '& "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) 'Dim myData As String = "{""file"": """ & Filename & """, ""question"": """ & Question & """, ""search_model"": ""ada"", ""model"": ""curie"", ""examples_context"": ""In 2017, U.S. life expectancy was 78.6 years."", ""examples"": [[""What Is human life expectancy In the United States?"", ""78 years.""]],""max_rerank"": 10, ""max_tokens"": 5, ""stop"": [""\n"", ""<|endoftext|>""]}" 'Dim myData As String = "{""file"": """ & Filename & """, ""question"": """ & Question & """, ""temperature"": " & Tempature.ToString & ", ""search_model"": ""davinci"", ""model"": ""davinci"", ""examples_context"": ""In 2017, U.S. life expectancy was 78.6 years."", ""examples"": [[""What Is human life expectancy In the United States?"", ""78 years.""]],""max_rerank"": 5, ""max_tokens"": " & Number & "}" ' , ""stop"": [""\n"", ""<|endoftext|>""] 'For fine tuned attempt, giving 400 error Dim myData As String = "{""prompt"": """ & prompt & """, ""temperature"": " & Tempature.ToString & ", ""model"": " & modelid & ", ""max_tokens"": " & Number & "}" ' , ""stop"": [""\n"", ""<|endoftext|>""] 'Dim myData As String = "{""prompt"": """ & prompt & """, ""temperature"": " & Tempature.ToString & ", ""max_tokens"": " & Number & "}" ' , ""stop"": [""\n"", ""<|endoftext|>""] Debug.WriteLine(myData) myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd Debug.WriteLine(myText) Return myText End Function Private Function TrainUsingFile(ByVal APIKey As String, ByVal Filename As String, ByVal model As String) Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("https://api.openai.com/v1/fine-tunes") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Bearer " & APIKey) '& "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) Dim myData As String = "{""training_file"": """ & Filename & """, ""model"": """ & model.ToString & """}" ' , ""stop"": [""\n"", ""<|endoftext|>""] Debug.WriteLine(myData) myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd Debug.WriteLine(myText) Return myText End Function Private Function AskAboutFile(ByVal APIKey As String, ByVal Question As String, ByVal Filename As String, ByVal Number As Integer, ByVal Tempature As Decimal) Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("https://api.openai.com/v1/answers") myReq.Method = "POST" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Bearer " & APIKey) '& "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) 'Dim myData As String = "{""file"": """ & Filename & """, ""question"": """ & Question & """, ""search_model"": ""ada"", ""model"": ""curie"", ""examples_context"": ""In 2017, U.S. life expectancy was 78.6 years."", ""examples"": [[""What Is human life expectancy In the United States?"", ""78 years.""]],""max_rerank"": 10, ""max_tokens"": 5, ""stop"": [""\n"", ""<|endoftext|>""]}" Dim myData As String = "{""file"": """ & Filename & """, ""question"": """ & Question & """, ""temperature"": " & Tempature.ToString & ", ""search_model"": ""davinci"", ""model"": ""davinci"", ""examples_context"": ""In 2017, U.S. life expectancy was 78.6 years."", ""examples"": [[""What Is human life expectancy In the United States?"", ""78 years.""]],""max_rerank"": 5, ""max_tokens"": " & Number & "}" ' , ""stop"": [""\n"", ""<|endoftext|>""] Debug.WriteLine(myData) myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd Debug.WriteLine(myText) Return myText End Function Private Function GetFineTunedList(ByVal APIKey As String, Optional FineTunedFile As String = "") As String Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("https://api.openai.com/v1/fine-tunes" & FineTunedFile) myReq.Method = "GET" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Bearer " & APIKey) '& "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd Debug.WriteLine(myText) Return myText End Function Private Function GetFileList(ByVal APIKey As String) As String Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("https://api.openai.com/v1/files") myReq.Method = "GET" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Bearer " & APIKey) '& "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd Debug.WriteLine(myText) Return myText End Function Private Function DeleteFileFromOpenAI(ByVal APIKey As String, Filename As String) As String Dim myReq As HttpWebRequest Dim myResp As HttpWebResponse myReq = HttpWebRequest.Create("https://api.openai.com/v1/files/" & Filename) myReq.Method = "DELETE" myReq.ContentType = "application/json" myReq.Headers.Add("Authorization", "Bearer " & APIKey) '& "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("username:password"))) myResp = myReq.GetResponse Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream) Dim myText As String myText = myreader.ReadToEnd Debug.WriteLine(myText) Return myText End Function Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click GetFileListNames(APIKeyTextBox.Text) GetFineTuneNames(APIKeyTextBox.Text) End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Dim Files As String = DeleteFileFromOpenAI(APIKeyTextBox.Text, ListBox1.SelectedItem.ToString.Split(" - ")(0)) Dim result = JsonConvert.DeserializeObject(Files) If CBool(result("deleted")) Then ListBox1.Items.Remove(ListBox1.SelectedItem) End If End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click 'UploadJSON(APIKeyTextBox.Text, "answers", My.Application.Info.DirectoryPath & "\" & "myfile.jsonl") UploadJSON(APIKeyTextBox.Text, "fine-tune", My.Application.Info.DirectoryPath & "\" & "myfile.jsonl") GetFileListNames(APIKeyTextBox.Text) End Sub Private Function GetFileListNames(APIKey As String) As Boolean Dim Files As String = GetFileList(APIKey) Dim result = JsonConvert.DeserializeObject(Files) For Each MyFiles In result("data") If Not ListBox1.Items.Contains(MyFiles("id") & " - " & MyFiles("filename")) Then ListBox1.Items.Add(MyFiles("id") & " - " & MyFiles("filename")) End If Next Return True End Function Private Function GetFineTuneNames(APIKey As String) As Boolean Dim Files As String = GetFineTunedList(APIKey) Dim result = JsonConvert.DeserializeObject(Files) For Each MyFiles In result("data") If Not ListBox1.Items.Contains(MyFiles("id") & " - " & MyFiles("filename")) Then ListBox1.Items.Add(MyFiles("id") & " - " & MyFiles("filename")) GetFineTunedList(APIKey, "/" & MyFiles("id")) End If Next Return True End Function Public Function UploadJSON(ByVal APIKey As String, ByVal Purpose As String, ByVal FileToUpload As String) Dim uploadUrl As String = "https://api.openai.com/v1/files" Dim wcc As WebClient = New WebClient() Dim bytes As Byte() = wcc.DownloadData(FileToUpload) Dim postParameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object)() Dim MyFileParam = New FileParameter(bytes, Path.GetFileName(FileToUpload), "application/octet-stream") '"application/octet-stream" MyFileParam.Purpose = Purpose postParameters.Add(FileToUpload, MyFileParam) Dim Mydict As New Dictionary(Of String, String) Mydict.Add("Authorization", "Bearer " & APIKey) Dim webResponse As HttpWebResponse = MultipartFormPost(uploadUrl, "curl/7.55.1", postParameters, "Authorization", "Bearer " & APIKeyTextBox.Text) Dim responseReader As StreamReader = New StreamReader(webResponse.GetResponseStream()) Dim returnResponseText = responseReader.ReadToEnd() webResponse.Close() Debug.WriteLine(returnResponseText) End Function Public Function UploadFineTunedJSON(ByVal APIKey As String, ByVal Purpose As String, ByVal FileToUpload As String) Dim uploadUrl As String = "https://api.openai.com/v1/fine-tunes" Dim wcc As WebClient = New WebClient() Dim bytes As Byte() = wcc.DownloadData(FileToUpload) Dim postParameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object)() Dim MyFileParam = New FileParameter(bytes, Path.GetFileName(FileToUpload), "application/octet-stream") '"application/octet-stream" MyFileParam.Purpose = Purpose postParameters.Add(FileToUpload, MyFileParam) Dim Mydict As New Dictionary(Of String, String) Mydict.Add("Authorization", "Bearer " & APIKey) Dim webResponse As HttpWebResponse = MultipartFormPost(uploadUrl, "curl/7.55.1", postParameters, "Authorization", "Bearer " & APIKeyTextBox.Text) Dim responseReader As StreamReader = New StreamReader(webResponse.GetResponseStream()) Dim returnResponseText = responseReader.ReadToEnd() webResponse.Close() Debug.WriteLine(returnResponseText) End Function Private Shared ReadOnly encoding As Encoding = Encoding.UTF8 Public Function MultipartFormPost(ByVal postUrl As String, ByVal userAgent As String, ByVal postParameters As Dictionary(Of String, Object), ByVal headerkey As String, ByVal headervalue As String) As HttpWebResponse Dim formDataBoundary As String = String.Format("----------{0:N}", Guid.NewGuid()) Dim contentType As String = "multipart/form-data; boundary=" & formDataBoundary Dim formData As Byte() = GetMultipartFormData(postParameters, formDataBoundary) Return PostForm(postUrl, userAgent, contentType, formData, headerkey, headervalue) End Function Private Function PostForm(ByVal postUrl As String, ByVal userAgent As String, ByVal contentType As String, ByVal formData As Byte(), ByVal headerkey As String, ByVal headervalue As String) As HttpWebResponse Dim request As HttpWebRequest = TryCast(WebRequest.Create(postUrl), HttpWebRequest) If request Is Nothing Then Throw New NullReferenceException("request is not a http request") End If request.Method = "POST" request.ContentType = contentType request.UserAgent = userAgent request.CookieContainer = New CookieContainer() request.ContentLength = formData.Length 'request.Headers.Add("purpose", "answers") request.Headers.Add(headerkey, headervalue) Using requestStream As Stream = request.GetRequestStream() requestStream.Write(formData, 0, formData.Length) requestStream.Close() End Using Return TryCast(request.GetResponse(), HttpWebResponse) End Function Private Function GetMultipartFormData(ByVal postParameters As Dictionary(Of String, Object), ByVal boundary As String) As Byte() Dim formDataStream As Stream = New System.IO.MemoryStream() Dim needsCLRF As Boolean = False For Each param In postParameters If needsCLRF Then formDataStream.Write(encoding.GetBytes(vbCrLf), 0, encoding.GetByteCount(vbCrLf)) needsCLRF = True If TypeOf param.Value Is FileParameter Then Dim fileToUpload As FileParameter = CType(param.Value, FileParameter) Dim header As String = String.Format("--{0}" & vbCrLf & "Content-Disposition: form-data; name=""purpose""" & vbCrLf & vbCrLf & fileToUpload.Purpose & vbCrLf & "--{0}" & vbCrLf & "Content-Disposition: form-data; name=""file""; filename=""myfile.jsonl""" & vbCrLf & "Content-Type: {3}" & vbCrLf & vbCrLf, boundary, param.Key, If(fileToUpload.FileName, "file"), If(fileToUpload.ContentType, "application/octet-stream")) ' param.Key formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header)) formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length) Else Dim postData As String = String.Format("--{0}" & vbCrLf & "Content-Disposition: form-data; name=""{1}""" & vbCrLf & vbCrLf & "{2}", boundary, param.Key, param.Value) formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData)) End If Next Dim footer As String = vbCrLf & "--" & boundary & "--" & vbCrLf formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer)) formDataStream.Position = 0 Dim formData As Byte() = New Byte(formDataStream.Length - 1) {} formDataStream.Read(formData, 0, formData.Length) formDataStream.Close() Return formData End Function Public Class FileParameter Public Property File As Byte() Public Property FileName As String Public Property ContentType As String Public Property Purpose As String Public Sub New(ByVal file As Byte()) Me.New(file, Nothing) End Sub Public Sub New(ByVal file As Byte(), ByVal filename As String) Me.New(file, filename, Nothing) End Sub Public Sub New(ByVal gfile As Byte(), ByVal gfilename As String, ByVal gcontenttype As String) File = gfile FileName = gfilename ContentType = gcontenttype End Sub Private Sub SetAIPurpose(ByVal gAIPurpose As String) Purpose = gAIPurpose End Sub End Class Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Tempature As Decimal = CDec(NumericUpDown2.Value) / 100D 'ListBox1.SelectedItem.ToString.Split(" - ")(0) 'Currently working on getting finetuning to work, it cost alot to finetune! Dim Response = CompleteUsingFile(APIKeyTextBox.Text, "davinci:ft-personal-2022-03-14-08-18-58", TextBox1.Text, Nothing, NumericUpDown1.Value, Tempature) Dim result = JsonConvert.DeserializeObject(Response) TextBox2.Text = result("choices")(0)("text").ToString.Replace("\n", vbCrLf) End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click Dim Response = TrainUsingFile(APIKeyTextBox.Text, ListBox1.SelectedItem.ToString.Split(" - ")(0), "davinci") Dim result = JsonConvert.DeserializeObject(Response) TextBox2.Text &= Response 'result("choices")(0)("text").ToString.Replace("\n", vbCrLf) End Sub End Class Class TestClass Public Shared Sub Test() Test(Enumerable.Range(0, 4).[Select](Function(i) New With {Key .Text = "Hello " & vbLf & i.ToString() })) Test(Enumerable.Range(0, 4).[Select](Function(i) JObject.FromObject(New With {Key .text = "Hello " & i.ToString(), Key .metadata = Environment.NewLine }))) Debug.WriteLine("Done") End Sub Public Shared Function Test(Of T)(ByVal items As IEnumerable(Of T)) As String 'Console.WriteLine(String.Format("Testing items of type {0}", GetType(T))) Dim sb = New StringBuilder() Using textWriter = New StringWriter(sb) JsonExtensions.ToNewlineDelimitedJson(textWriter, items) End Using Dim json = sb.ToString() Return (json) Dim newItems = JsonExtensions.FromNewlineDelimitedJson(Of T)(New StringReader(json)).ToList() Assert.IsTrue(newItems.Count = items.Count()) Assert.IsTrue(newItems.Count = json.CountSubstrings(vbLf)) 'Console.WriteLine("Serialized JSON has the correct count of items and lines: {0}", newItems.Count) End Function End Class Partial Module JsonExtensions Sub ToNewlineDelimitedJson(Of T)(ByVal stream As Stream, ByVal items As IEnumerable(Of T)) Using textWriter = New StreamWriter(stream, New UTF8Encoding(False, True), 1024, True) ToNewlineDelimitedJson(textWriter, items) End Using End Sub Sub ToNewlineDelimitedJson(Of T)(ByVal textWriter As TextWriter, ByVal items As IEnumerable(Of T)) Dim serializer = JsonSerializer.CreateDefault() For Each item In items Using writer = New JsonTextWriter(textWriter) With { .Formatting = Formatting.None, .CloseOutput = False } serializer.Serialize(writer, item) End Using textWriter.Write(vbLf) Next End Sub End Module Partial Module JsonExtensions Iterator Function FromNewlineDelimitedJson(Of T)(ByVal reader As TextReader) As IEnumerable(Of T) Using jsonReader = New JsonTextReader(reader) With { .CloseInput = False, .SupportMultipleContent = True } Dim serializer = JsonSerializer.CreateDefault() While jsonReader.Read() If jsonReader.TokenType = JsonToken.Comment Then Continue While Yield serializer.Deserialize(Of T)(jsonReader) End While End Using End Function End Module Public Class AssertionFailedException Inherits System.Exception Public Sub New() MyBase.New() End Sub Public Sub New(ByVal s As String) MyBase.New(s) End Sub End Class Module Assert Sub IsTrue(ByVal Text As Boolean) If Text = False Then Throw New AssertionFailedException("failed") End Sub End Module Module TextExtensions <Extension()> Function CountSubstrings(ByVal source As String, ByVal substring As String) As Integer Dim count As Integer = 0, n As Integer = 0 If substring <> "" Then While (CSharpImpl.__Assign(n, source.IndexOf(substring, n, StringComparison.InvariantCulture))) <> -1 n += substring.Length count += 1 End While End If Return count End Function Private Class CSharpImpl <Obsolete("Please refactor calling code to use normal Visual Basic assignment")> Shared Function __Assign(Of T)(ByRef target As T, Text As T) As T target = Text Return Text End Function End Class End Module |