void OnGUI() { GUILayout.Space(20); scrollPos = EditorGUILayout.BeginScrollView(scrollPos); EditorGUI.BeginChangeCheck(); editorAreaText = EditorGUILayout.TextArea(editorAreaText, GUILayout.ExpandHeight(true)); bool changes = EditorGUI.EndChangeCheck(); EditorGUILayout.EndScrollView(); if (changes) { message = ""; tightJSON = ""; escapedJSON = ""; } GUILayout.Space(10); if (GUILayout.Button("Trim, validate and prettify above JSON object or array")) { string trimmedEditorText = editorAreaText.Trim(); if (trimmedEditorText.Length < editorAreaText.Length) { editorAreaText = trimmedEditorText; // Needed in case of errors, so that line numbers will match GUIUtility.keyboardControl = 0; GUIUtility.hotControl = 0; EditorUtility.SetDirty(this); } if (string.IsNullOrEmpty(trimmedEditorText)) { message = "Input is empty"; tightJSON = ""; escapedJSON = ""; } else { object objectOrError = findAndGetJSONOrJArray(trimmedEditorText); if (objectOrError == null) { message = "Can't find JSON start from input"; tightJSON = ""; escapedJSON = ""; } else if (objectOrError is string) { message = "Invalid input: " + ((string)(objectOrError)); tightJSON = ""; escapedJSON = ""; } else { CreateStringSettings prettySettings = new CreateStringSettings() { HumanReadable = true, IndentUsingTab = true, NewLine = CreateStringSettings.NewLineTypes.LF // \r characters (may be part of environment default) seem to be problem in editor textarea, causing invisible linefeeds, so using plain \n }; if (objectOrError is JSON[]) { JSON[] jsons = (JSON[])(objectOrError); if (jsons.Length == 1) { message = "JSON is valid. Top level JSON key/value pair count = " + jsons[0].Count; editorAreaText = jsons[0].CreateString(prettySettings) + "\n"; tightJSON = jsons[0].CreateString(new CreateStringSettings() { HumanReadable = false }); escapedJSON = "\"" + getEscapedString(tightJSON) + "\""; } else { message = "JSONs are valid. JSON object count = " + jsons.Length; editorAreaText = ""; tightJSON = ""; for (int n = 0; n < jsons.Length; n++) { editorAreaText += jsons[n].CreateString(prettySettings) + "\n"; if (n < jsons.Length - 1) { editorAreaText += '\n'; } tightJSON += jsons[n].CreateString(new CreateStringSettings() { HumanReadable = false }); } escapedJSON = "\"" + getEscapedString(tightJSON) + "\""; } } else { JArray jArray = (JArray)(objectOrError); message = "JSON Array is valid. Top level array length = " + jArray.Length; editorAreaText = jArray.CreateString(prettySettings) + "\n"; tightJSON = jArray.CreateString(new CreateStringSettings() { HumanReadable = false }); escapedJSON = "\"" + getEscapedString(tightJSON) + "\""; } GUIUtility.keyboardControl = 0; GUIUtility.hotControl = 0; EditorUtility.SetDirty(this); } } } GUILayout.Space(20); GUILayout.Label(message); GUILayout.Space(20); EditorGUI.BeginDisabledGroup(tightJSON.Length == 0); GUILayout.Label("JSON formatted and encoded string (" + tightJSON.Length + " bytes):"); GUILayout.Space(5); EditorGUILayout.SelectableLabel(tightJSON, EditorStyles.textField, GUILayout.Height(lineHeight + 2)); GUILayout.Space(15); GUILayout.Label("Above string with escapes (to be used for example directly in c# source code):"); GUILayout.Space(5); EditorGUILayout.SelectableLabel(escapedJSON, EditorStyles.textField, GUILayout.Height(lineHeight + 2)); GUILayout.Space(20); EditorGUI.EndDisabledGroup(); }