Skip to content

Handle UTF8 strings better when JSON encoding #276

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

Merged
merged 2 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
shell: 'script -q -e -c "bash {0}"'
run: |
docker ps
docker logs cosmos_openc3-cosmos-init_1
docker logs cosmos_openc3-operator_1
# Build a test plugin for playwright and a copy so we can 'upgrade'
- name: Build plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
-->

Expand Down Expand Up @@ -1664,7 +1664,8 @@ export default {
this.showSaveAs = true
},
saveAsFilename(filename) {
this.filename = filename
this.filename = filename.split('*')[0]
this.currentFilename = null
if (this.tempFilename) {
Api.post(`/script-api/scripts/${this.tempFilename}/delete`)
this.tempFilename = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
-->

Expand Down Expand Up @@ -265,8 +265,8 @@ export default {
return found
},
success: function () {
// Only process the success call if a file is selected
if (this.selectedFile !== null) {
// Only process the success call if a file is selected and no error
if ((this.selectedFile !== null) && (this.error === null)) {
if (this.type === 'open') {
this.openSuccess()
} else {
Expand Down
11 changes: 6 additions & 5 deletions openc3-cosmos-script-runner-api/app/models/running_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ def start(procedure_name)
# Use cached instrumentation
instrumented_script = instrumented_cache
cached = true
OpenC3::Store.publish(["script-api", "running-script-channel:#{RunningScript.instance.id}"].compact.join(":"), JSON.generate({ type: :file, filename: procedure_name, text: text, breakpoints: breakpoints }))
OpenC3::Store.publish(["script-api", "running-script-channel:#{RunningScript.instance.id}"].compact.join(":"), JSON.generate({ type: :file, filename: procedure_name, text: text.to_utf8, breakpoints: breakpoints }))
else
# Retrieve file
text = ::Script.body(RunningScript.instance.scope, procedure_name)
OpenC3::Store.publish(["script-api", "running-script-channel:#{RunningScript.instance.id}"].compact.join(":"), JSON.generate({ type: :file, filename: procedure_name, text: text, breakpoints: breakpoints }))
raise "Unable to retrieve: #{procedure_name}" unless text
OpenC3::Store.publish(["script-api", "running-script-channel:#{RunningScript.instance.id}"].compact.join(":"), JSON.generate({ type: :file, filename: procedure_name, text: text.to_utf8, breakpoints: breakpoints }))

# Cache instrumentation into RAM
instrumented_script = RunningScript.instrument_script(text, path, true)
Expand Down Expand Up @@ -403,7 +404,7 @@ def initialize(id, scope, name, disconnect)
breakpoints = @@breakpoints[filename]&.filter { |_, present| present }&.map { |line_number, _| line_number - 1 } # -1 because frontend lines are 0-indexed
breakpoints ||= []
OpenC3::Store.publish(["script-api", "running-script-channel:#{@id}"].compact.join(":"),
JSON.generate({ type: :file, filename: @filename, scope: @scope, text: @body, breakpoints: breakpoints }))
JSON.generate({ type: :file, filename: @filename, scope: @scope, text: @body.to_utf8, breakpoints: breakpoints }))
if name.include?("suite")
# Process the suite file in this context so we can load it
# TODO: Do we need to worry about success or failure of the suite processing?
Expand Down Expand Up @@ -1300,12 +1301,12 @@ def load_file_into_script(filename)
if cached
# @active_script.setPlainText(cached.gsub("\r", ''))
@body = cached
OpenC3::Store.publish(["script-api", "running-script-channel:#{@id}"].compact.join(":"), JSON.generate({ type: :file, filename: filename, text: @body, breakpoints: breakpoints }))
OpenC3::Store.publish(["script-api", "running-script-channel:#{@id}"].compact.join(":"), JSON.generate({ type: :file, filename: filename, text: @body.to_utf8, breakpoints: breakpoints }))
else
text = ::Script.body(@scope, filename)
@@file_cache[filename] = text
@body = text
OpenC3::Store.publish(["script-api", "running-script-channel:#{@id}"].compact.join(":"), JSON.generate({ type: :file, filename: filename, text: @body, breakpoints: breakpoints }))
OpenC3::Store.publish(["script-api", "running-script-channel:#{@id}"].compact.join(":"), JSON.generate({ type: :file, filename: filename, text: @body.to_utf8, breakpoints: breakpoints }))
end

# @active_script.stop_highlight
Expand Down
10 changes: 6 additions & 4 deletions openc3/lib/openc3/io/json_rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

require 'json'
require 'date'
require 'openc3/core_ext/string'

class Object
def as_json(options = nil) #:nodoc:
Expand Down Expand Up @@ -57,10 +58,11 @@ class String
NON_ASCII_PRINTABLE = /[^\x21-\x7e\s]/

def as_json(options = nil)
if NON_ASCII_PRINTABLE.match?(self)
self.to_json_raw_object
as_utf8 = self.dup.force_encoding('UTF-8')
if as_utf8.valid_encoding?
return as_utf8
else
self
return self.to_json_raw_object
end
end #:nodoc:
end
Expand Down