Coder Social home page Coder Social logo

Comments (8)

zeertzjq avatar zeertzjq commented on May 27, 2024

:h lua-table

Like indices, the value of a table field can be of any type (except `nil`). In
particular, because functions are first-class values, table fields may contain
functions. Thus tables may also carry methods (see |lua-function-define|).

from neovim.

aktersnurra avatar aktersnurra commented on May 27, 2024

Then I suppose the documentation needs to be updated since it states:

:h vim.json.decode
vim.json.decode({str}, {opts})                             *vim.json.decode()*
    Decodes (or "unpacks") the JSON-encoded {str} to a Lua object.

    • Decodes JSON "null" as |vim.NIL| (controllable by {opts}, see below).
    • Decodes empty object as |vim.empty_dict()|.
    • Decodes empty array as `{}` (empty Lua table).

    Example: >lua
        vim.print(vim.json.decode('{"bar":[],"foo":{},"zub":null}'))
        -- { bar = {}, foo = vim.empty_dict(), zub = vim.NIL }
<

    Parameters: ~
      • {str}   (`string`) Stringified JSON data.
      • {opts}  (`table<string,any>?`) Options table with keys:
                • luanil: (table) Table with keys:
                  • object: (boolean) When true, converts `null` in JSON
                    objects to Lua `nil` instead of |vim.NIL|.
                  • array: (boolean) When true, converts `null` in JSON arrays
                    to Lua `nil` instead of |vim.NIL|.


from neovim.

justinmk avatar justinmk commented on May 27, 2024

Then I suppose the documentation needs to be updated since it states:

Which part of the doc is wrong?

You can try your expected result directly in Lua, without involving vim.json.decode():

lua vim.print({ zub = nil, bar = {}, foo = vim.empty_dict() })

that prints:

{                                                                                                                                                                                                                    
  bar = {},                                                                                                                                                                                                          
  foo = vim.empty_dict()                                                                                                                                                                                             
}

Setting a table key to nil , removes the key.

from neovim.

aktersnurra avatar aktersnurra commented on May 27, 2024

Yeah, you are right, the documentation is not wrong. It was my assumption of how it would work that was wrong, since I expected that lua-tables could hold nil values. So when I read this:

object: (boolean) When true, converts `null` in JSON
                    objects to Lua `nil` instead of |vim.NIL|.

I did not expect them to disappear.

But is it not strange that if I run this configuration:

vim.json.decode('{"bar":[],"foo":{},"zub":null}', { luanil = { object = false, array = false }})

I still get:

{
  bar = {},
  foo = vim.empty_dict()
}

Anyway, thanks for the help, much appreciated.

from neovim.

justinmk avatar justinmk commented on May 27, 2024

But is it not strange that if I run this configuration:

vim.json.decode('{"bar":[],"foo":{},"zub":null}', { luanil = { object = false, array = false }})

I still get:

{
  bar = {},
  foo = vim.empty_dict()
}

that does seem like a bug

from neovim.

zeertzjq avatar zeertzjq commented on May 27, 2024

It seems that these aren't actually boolean keys. Setting them to any value except nil behaves like true.

from neovim.

zeertzjq avatar zeertzjq commented on May 27, 2024

Upstream doesn't have luanil. It's an Nvim addition.

from neovim.

clason avatar clason commented on May 27, 2024

Yes, that was my point. But never mind.

from neovim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.