' Gambas class file
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
Export
'' Encode in JSON format in a human readable way. Based on a Laurent tool
Static Public Function Encode2(vData As Variant) As String
 
  Dim sInput As String
  Dim sOutput As String
  Dim iStream As Stream
 
  Dim $sReadChar As String
  Dim $iTab As Integer
  Dim $bQuote, $bBracket As Boolean
 
  sInput = JSON.Encode(vData)
 
  iStream = Open String sInput For Read
 
  While (Not Eof(iStream))
    $sReadChar = Read #iStream, 1
    If ($sReadChar = "{" And Not $bQuote) Then
      $iTab += 1
      $sReadChar &= "\n" & Space$($iTab * 2)
    Else If ($sReadChar = "}" And Not $bQuote) Then
      $iTab -= 1
      $sReadChar = "\n" & Space$($iTab * 2) & $sReadChar
    Else If ($sReadChar = "\"") Then
      $bQuote = Not $bQuote
    Else If ($sReadChar = "[") Then
      $bBracket = True
    Else If ($sReadChar = "]") Then
      $bBracket = False
    Else If ($sReadChar = ":" And Not $bQuote) Then
      $sReadChar &= " "
    Else If ($sReadChar = "," And Not $bQuote) Then
      If (Not $bBracket) Then
        $sReadChar &= "\n" & Space$($iTab * 2)
      Else
        $sReadChar &= " "
      Endif
    Endif
    sOutput &= $sReadChar
  Wend
 
  Close iStream
  ' replace null with ""
  sOutput = Replace(sOutput, " null", " \"\"")
 
  Return sOutput
 
End[{
  "Entity": "line", "Index": 0, "Block": false, "Level": 1, "Color": 1, "Style": 1, "Weight": 2, "Points": [[236, 37],
  [286, 37]]
},
{
  "Entity": "line",
  "Index": 1,
  "Block": false,
  "Level": 1,
  "Color": 1,
  "Style": 1,
  "Weight": 2,
  "Points": [[251, 35],
  [251, 38]]
},
{
  "Entity": "line",
  "Index": 2,
  "Block": false,
  "Level": 1,
  "Color": 1,
  "Style": 1,
  "Weight": 2,
  "Points": [[236, 41],
  [286, 41]]
}]![[Imagen: 1RCrUjg.png]](https://i.imgur.com/1RCrUjg.png)
(30-04-2022, 19:35)Shordi escribió: Le he echado un ojo, pero ni idea, oiga. Sólo una cosa: en mi aparato el fondo para dibujar es negro y las líneas rojas... con lo que el cursor, negro, no se vé.
(30-04-2022, 19:35)Shordi escribió: Sólo una cosa: en mi aparato el fondo para dibujar es negro y las líneas rojas... con lo que el cursor, negro, no se vé.
[{
  "Entity": "circle",
  "Index": 0,
  "Block": false,
  "Level": 1,
  "Color": 1,
  "Style": 1,
  "Weight": 2,
  "Points": [[409, 290], [581, 188]]
},
{
  "Entity": "polyline",
  "Index": 1,
  "Block": false,
  "Level": 1,
  "Color": 1,
  "Style": 1,
  "Weight": 2,
  "Points": [[282, 115], [732, 67], [974, 233], [892, 493], [793, 261], [694, 588]]
},
{
  "Entity": "line",
  "Index": 2,
  "Block": false,
  "Level": 1,
  "Color": 2,
  "Style": 1,
  "Weight": 2,
  "Points": [[85, 62], [786, 401]]
}]Static Public Function Encode2(vData As Variant) As String
  Dim sInput As String
  Dim sOutput As String
  Dim iStream As Stream
  Dim $sReadChar As String
  Dim $iTab As Integer
  Dim $bQuote, $bBracket As Boolean
  Dim $BracketLevel As Integer            ' Nuevo
  sInput = JSON.Encode(vData)
  iStream = Open String sInput For Read
  While (Not Eof(iStream))
    $sReadChar = Read #iStream, 1
    If ($sReadChar = "{" And Not $bQuote) Then
      $iTab += 1
      $sReadChar &= "\n" & Space$($iTab * 2)
    Else If ($sReadChar = "}" And Not $bQuote) Then
      $iTab -= 1
      $sReadChar = "\n" & Space$($iTab * 2) & $sReadChar
    Else If ($sReadChar = "\"") Then
      $bQuote = Not $bQuote
    Else If ($sReadChar = "[") Then
      Inc $BracketLevel
      $bBracket = True
    Else If ($sReadChar = "]") Then
      $bBracket = False
      Dec $BracketLevel
    Else If ($sReadChar = ":" And Not $bQuote) Then
      $sReadChar &= " "
    Else If ($sReadChar = "," And Not $bQuote) Then
      If $BracketLevel > 1 Then             ' solo agrego un salto de linea si no hay abierto nuevos brackets
        $sReadChar &= " "                   
      Else
        $sReadChar &= "\n" & Space$($iTab * 2) 
      End If
    Endif
    sOutput &= $sReadChar
  Wend
  Close iStream
  ' replace null with ""
  sOutput = Replace(sOutput, " null", " \"\"")
  Return sOutput
End(01-05-2022, 00:17)tercoide escribió: Idem
(01-05-2022, 00:17)tercoide escribió: este es el codigo que resuelve el problemita (me tomo un buen rato pues estaba tricky)
![[Imagen: 8MbqCo0.png]](https://i.imgur.com/8MbqCo0.png)