Seite 1 von 1

Beautifier: JSON und XML formatieren

Verfasst: 23.10.2014 23:25
von Kiffi
Holleri du dödl di,

klein, aber dennoch ganz nützlich: Beautifier

Nachfolgender Code formatiert JSON und XML, sodass sie lesbarer sind:

Beautifier.pbf:

Code: Alles auswählen

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Enumeration FormWindow
  #frmMain
EndEnumeration

Enumeration FormGadget
  #cmdBeautify
  #edIn
  #edOut
  #Splitter_0
EndEnumeration

Declare ResizeGadgetsfrmMain()

Procedure OpenfrmMain(x = 0, y = 0, width = 590, height = 480)
  OpenWindow(#frmMain, x, y, width, height, "Beautifier", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  ButtonGadget(#cmdBeautify, 480, 440, 100, 30, "Beautify")
  EditorGadget(#edIn, 10, 10, 570, 210, #PB_Editor_WordWrap)
  EditorGadget(#edOut, 10, 229, 570, 201)
  SplitterGadget(#Splitter_0, 10, 10, 570, 420, #edIn, #edOut)
  SetGadgetState(#Splitter_0, 210)
EndProcedure

Procedure ResizeGadgetsfrmMain()
  Protected FormWindowWidth, FormWindowHeight
  FormWindowWidth = WindowWidth(#frmMain)
  FormWindowHeight = WindowHeight(#frmMain)
  ResizeGadget(#cmdBeautify, FormWindowWidth - 110, FormWindowHeight - 40, 100, 30)
  ResizeGadget(#Splitter_0, 10, 10, FormWindowWidth - 20, FormWindowHeight - 60)
EndProcedure
Beautifier.pb:

Code: Alles auswählen

EnableExplicit

XIncludeFile "Beautifier.pbf"

OpenfrmMain()

Procedure BeautifyXml()
	
	Protected Xml
	
	Xml = ParseXML(#PB_Any, GetGadgetText(#edIn))
	
	If Xml And XMLStatus(Xml) = #PB_XML_Success
		FormatXML(Xml, #PB_XML_ReFormat)
		SetGadgetText(#edOut, ComposeXML(Xml))
		FreeXML(Xml)
	Else
		SetGadgetText(#edOut, XMLError(Xml) + #CRLF$ + "Line: " + Str(XMLErrorLine(Xml)) + #CRLF$ + "Position: " + Str(XMLErrorPosition(Xml)))
	EndIf
	
EndProcedure

Procedure BeautifyJson()
	
	Protected Json
	
	Json = ParseJSON(#PB_Any, GetGadgetText(#edIn))
	
	If Json
		SetGadgetText(#edOut, ComposeJSON(Json, #PB_JSON_PrettyPrint))
		FreeJSON(Json)
	Else
		SetGadgetText(#edOut, JSONErrorMessage() + #CRLF$ + "Line: " + Str(JSONErrorLine()) + #CRLF$ + "Position: " + Str(JSONErrorPosition()))
	EndIf
	
EndProcedure

Procedure cmdBeautify_Event()
	
	Protected TextToBeautify.s = GetGadgetText(#edIn)
	
	While Left(TextToBeautify, 1) = Chr(32) Or Left(TextToBeautify, 1) = #TAB$
		TextToBeautify = Trim(TextToBeautify, Chr(32))
		TextToBeautify = Trim(TextToBeautify, #TAB$)
	Wend	
	
	Select Left(TextToBeautify, 1)
		Case "<"
			BeautifyXml()
		Case "[", "{"
			BeautifyJson()
		Default
			MessageRequester("Beautifier", "?")
	EndSelect
	
EndProcedure

BindEvent(#PB_Event_SizeWindow, @ResizeGadgetsfrmMain())
BindGadgetEvent(#cmdBeautify, @cmdBeautify_Event(), #PB_EventType_LeftClick)
SetActiveGadget(#edIn)

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Und so sieht das ganze in Aktion aus:

JSON:
Bild

XML:
Bild

Grüße ... Peter

Re: Beautifier: JSON und XML formatieren

Verfasst: 24.10.2014 00:11
von ts-soft
:allright:

Ein paar Vorschläge zur Verbesserung:
Den Splitter nicht mittig setzen sondern ca. 1/3 zu 2/3,
da der obere Text im allgemein immer kürzer ist. Läßt
sich tatsächlich im FormDesigner einstellen, hab extra
geguckt :wink:

Load- und Save-Button wären vielleicht auch sinnvoll,
man will ja nicht immer erst einen anderen Editor öffnen.

Gruß
Thomas