Jump to content

Check if JSON is correct


srslyyyy

Recommended Posts

  • Scripting Moderators

Hello. I need a way to check if JSON file is correct, and if some values are boolean/number etc. About 2nd i know that i can simply use type function.

JSON file is on client-side, and user settings are saved there (visuals).

Link to comment
  • Moderators

1. Open your browser.

2. Right-mouse button > inspector

3. Go to the console tab.

4. Copy the JSON.parse method.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

 

5. Place your json string as an argument.

JSON.parse("the string")

 

6. Copy and past the code in to the console and run it.

 

You have now loaded your JSON to it's origin. Or rather live version of it. Now you can inspect.

 

 

 

 

  • Like 1
Link to comment
  • Scripting Moderators
5 hours ago, IIYAMA said:

1. Open your browser.

2. Right-mouse button > inspector

3. Go to the console tab.

4. Copy the JSON.parse method.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

 

5. Place your json string as an argument.

JSON.parse("the string")

 

6. Copy and past the code in to the console and run it.

 

You have now loaded your JSON to it's origin. Or rather live version of it. Now you can inspect.

 

 

 

 

Yes i know that i can check it via browser. 

I've used site to it - https://jsonlint.com

But i need to check if JSON is correct via script. So it will not mess up settings like when player change something in it - manually.

Link to comment

To check if JSON is valid itself, all you need to do is check whether the result of fromJSON is not a nil. If you want to make sure the actual values within that JSON are correct and within bounds, you'll simply need to make your own function which tests every part of the JSON, makes sure the required keys are present or assign default values to them, make sure the values are within the bounds, etc.

function checkJson(text)
  local theTable = fromJSON(text)
  if not theTable then return false end -- it's not valid JSON
  if not theTable.some_required_key then return false end -- doesn't have the required key-value pair
  if theTable.some_required_key > maximum_allowed_value then return false end -- actual value exceeds allowed value
  if theTable.some_other_key and theTable.some_other_key > maximum_allowed_value then theTable.some_optional_key = maximum_allowed_value end -- update the value to be within the bounds if it would otherwise be outside it
  -- etc
  return theTable -- return the updated table if changes were made
end

function loadMyJson()
  local f = fileOpen("settings.json")
  local json = fileRead(f, fileGetSize(f))
  fileClose(f)
  local settings = checkJson(json)
  if not settings then
    outputChatBox("Could not load settings.json. Loading defaults...")
    settings = g_DefaultSettings
  end
  -- etc
end

If you simply don't want people to change the file manually at all, you could encodeString with TEA, or encodeString with TEA a SHA256 hash of it and store it alongside, and check if the hashes match. The person won't be able to generate a valid hash if they don't know how the TEA secret. This is in effect similar to what a HMAC does but I don't think MTA has HMAC functions built in. Naturally, if you choose the latter, the safest bet would be to have the server send the tea secret via an event rather than having that secret hardcoded in the script file. It would still be possible to lift it out of memory either way, but at least if it's on the server it can't be somehow extracted out of the file offline, through decrypting, decompiling or whatever.

  • Like 2
Link to comment
  • Moderators
54 minutes ago, majqq said:

Yes i know that i can check it via browser. 

I've used site to it - https://jsonlint.com

But i need to check if JSON is correct via script. So it will not mess up settings like when player change something in it - manually.

That is not why I recommend using the browser. Sure a linter can tell you if it is broken or not. But it doesn't tell you anything about the object types.

Breaking your JSON is annoying, but that doesn't happen as often as having your format being ruined by accidentally creating a mixed table.

Your browser will tell you for each object if you are dealing with an array or an object. When it is ruined, you are not being able access your arrays formats as easy any more. Those must be strictly maintained.

_________________________

About your main issue.

How about you write a validator?

validator = {
  
{
 key = "money",
 func = function (value)
    return type(value) == "number"
 end
},
{
 key = "name",
 func = function (value) 
    return type(value) == "string"
 end
},


}

 

 

 

 

 

 

 

Edited by IIYAMA
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...