How can I get ruby's JSON to follow object references like Pry/PP?
I've stared at this so long I'm going in circles...
I'm using the rbvmomi gem, and in Pry, when I display an object, it recurses down thru the structure showing me the nested objects - but to_json seems to "dig down" into some objects, but just dump the reference for others> Here's an example:
[24] pry(main)> g
=> [GuestNicInfo(
connected: true,
deviceConfigId: 4000,
dynamicProperty: ,
ipAddress: ["10.102.155.146"],
ipConfig: NetIpConfigInfo(
dynamicProperty: ,
ipAddress: [NetIpConfigInfoIpAddress(
dynamicProperty: ,
ipAddress: "10.102.155.146",
prefixLength: 20,
state: "preferred"
)]
),
macAddress: "00:50:56:a0:56:9d",
network: "F5_Real_VM_IPs"
)]
[25] pry(main)> g.to_json
=> "["#<RbVmomi::VIM::GuestNicInfo:0x000000085ecc68>"]"
Pry apparently just uses a souped-up pp, and while "pp g" gives me close to what I want, I'm kinda steering as hard as I can toward json so that I don't need a custom parser to load up and manipulate the results.
The question is - how can I get the json module to dig down like pp does? And if the answer is "you can't" - any other suggestions for achieving the goal? I'm not married to json - if I can get the data serialized and read it back later (without writing something to parse pp output... which may already exist and I should look for it), then it's all win.
My "real" goal here is to slurp up a bunch of info from our vsphere stuff via rbvmomi so that I can do some network/vm analysis on it, which is why I'd like to get it in a nice machine-parsed format. If I'm doing something stupid here and there's an easier way to go about this - lay it on me, I'm not proud. Thank you all for your time and attention.
Update: Based on Arnie's response, I added this monkeypatch to my script:
class RbVmomi::BasicTypes::DataObject
def to_json(*args)
h = self.props
m = h.merge({ JSON.create_id => self.class.name })
m.to_json(*args)
end
end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
json ruby pry rbvmomi
add a comment |
I've stared at this so long I'm going in circles...
I'm using the rbvmomi gem, and in Pry, when I display an object, it recurses down thru the structure showing me the nested objects - but to_json seems to "dig down" into some objects, but just dump the reference for others> Here's an example:
[24] pry(main)> g
=> [GuestNicInfo(
connected: true,
deviceConfigId: 4000,
dynamicProperty: ,
ipAddress: ["10.102.155.146"],
ipConfig: NetIpConfigInfo(
dynamicProperty: ,
ipAddress: [NetIpConfigInfoIpAddress(
dynamicProperty: ,
ipAddress: "10.102.155.146",
prefixLength: 20,
state: "preferred"
)]
),
macAddress: "00:50:56:a0:56:9d",
network: "F5_Real_VM_IPs"
)]
[25] pry(main)> g.to_json
=> "["#<RbVmomi::VIM::GuestNicInfo:0x000000085ecc68>"]"
Pry apparently just uses a souped-up pp, and while "pp g" gives me close to what I want, I'm kinda steering as hard as I can toward json so that I don't need a custom parser to load up and manipulate the results.
The question is - how can I get the json module to dig down like pp does? And if the answer is "you can't" - any other suggestions for achieving the goal? I'm not married to json - if I can get the data serialized and read it back later (without writing something to parse pp output... which may already exist and I should look for it), then it's all win.
My "real" goal here is to slurp up a bunch of info from our vsphere stuff via rbvmomi so that I can do some network/vm analysis on it, which is why I'd like to get it in a nice machine-parsed format. If I'm doing something stupid here and there's an easier way to go about this - lay it on me, I'm not proud. Thank you all for your time and attention.
Update: Based on Arnie's response, I added this monkeypatch to my script:
class RbVmomi::BasicTypes::DataObject
def to_json(*args)
h = self.props
m = h.merge({ JSON.create_id => self.class.name })
m.to_json(*args)
end
end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
json ruby pry rbvmomi
add a comment |
I've stared at this so long I'm going in circles...
I'm using the rbvmomi gem, and in Pry, when I display an object, it recurses down thru the structure showing me the nested objects - but to_json seems to "dig down" into some objects, but just dump the reference for others> Here's an example:
[24] pry(main)> g
=> [GuestNicInfo(
connected: true,
deviceConfigId: 4000,
dynamicProperty: ,
ipAddress: ["10.102.155.146"],
ipConfig: NetIpConfigInfo(
dynamicProperty: ,
ipAddress: [NetIpConfigInfoIpAddress(
dynamicProperty: ,
ipAddress: "10.102.155.146",
prefixLength: 20,
state: "preferred"
)]
),
macAddress: "00:50:56:a0:56:9d",
network: "F5_Real_VM_IPs"
)]
[25] pry(main)> g.to_json
=> "["#<RbVmomi::VIM::GuestNicInfo:0x000000085ecc68>"]"
Pry apparently just uses a souped-up pp, and while "pp g" gives me close to what I want, I'm kinda steering as hard as I can toward json so that I don't need a custom parser to load up and manipulate the results.
The question is - how can I get the json module to dig down like pp does? And if the answer is "you can't" - any other suggestions for achieving the goal? I'm not married to json - if I can get the data serialized and read it back later (without writing something to parse pp output... which may already exist and I should look for it), then it's all win.
My "real" goal here is to slurp up a bunch of info from our vsphere stuff via rbvmomi so that I can do some network/vm analysis on it, which is why I'd like to get it in a nice machine-parsed format. If I'm doing something stupid here and there's an easier way to go about this - lay it on me, I'm not proud. Thank you all for your time and attention.
Update: Based on Arnie's response, I added this monkeypatch to my script:
class RbVmomi::BasicTypes::DataObject
def to_json(*args)
h = self.props
m = h.merge({ JSON.create_id => self.class.name })
m.to_json(*args)
end
end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
json ruby pry rbvmomi
I've stared at this so long I'm going in circles...
I'm using the rbvmomi gem, and in Pry, when I display an object, it recurses down thru the structure showing me the nested objects - but to_json seems to "dig down" into some objects, but just dump the reference for others> Here's an example:
[24] pry(main)> g
=> [GuestNicInfo(
connected: true,
deviceConfigId: 4000,
dynamicProperty: ,
ipAddress: ["10.102.155.146"],
ipConfig: NetIpConfigInfo(
dynamicProperty: ,
ipAddress: [NetIpConfigInfoIpAddress(
dynamicProperty: ,
ipAddress: "10.102.155.146",
prefixLength: 20,
state: "preferred"
)]
),
macAddress: "00:50:56:a0:56:9d",
network: "F5_Real_VM_IPs"
)]
[25] pry(main)> g.to_json
=> "["#<RbVmomi::VIM::GuestNicInfo:0x000000085ecc68>"]"
Pry apparently just uses a souped-up pp, and while "pp g" gives me close to what I want, I'm kinda steering as hard as I can toward json so that I don't need a custom parser to load up and manipulate the results.
The question is - how can I get the json module to dig down like pp does? And if the answer is "you can't" - any other suggestions for achieving the goal? I'm not married to json - if I can get the data serialized and read it back later (without writing something to parse pp output... which may already exist and I should look for it), then it's all win.
My "real" goal here is to slurp up a bunch of info from our vsphere stuff via rbvmomi so that I can do some network/vm analysis on it, which is why I'd like to get it in a nice machine-parsed format. If I'm doing something stupid here and there's an easier way to go about this - lay it on me, I'm not proud. Thank you all for your time and attention.
Update: Based on Arnie's response, I added this monkeypatch to my script:
class RbVmomi::BasicTypes::DataObject
def to_json(*args)
h = self.props
m = h.merge({ JSON.create_id => self.class.name })
m.to_json(*args)
end
end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
json ruby pry rbvmomi
json ruby pry rbvmomi
edited Nov 21 '18 at 16:29
divibisan
4,35381531
4,35381531
asked Jun 27 '14 at 8:09
Tom Bortels
8816
8816
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The .to_json
works in a recursive manner, the default behavior is defined as:
Converts this object to a string (calling to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method to_json was defined for some object.
json
library has added some implementation for some common classes (check the left hand side of this documentation), such as Array
, Range
, DateTime
.
For an array, to_json
first convert all the elements to json object, concat then together, and then add the array mark [
/]
.
For your case, you need to define your customized to_json
method for GuestNicInfo
, NetIpConfigInfo
and NetIpConfigInfoIpAddress
. I don't know your implementation about these three classes, so I wrote a example to demonstrate how to achieve this:
require 'json'
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
end
data = [MyClass.new(1, "foobar")]
puts data.to_json
#=> ["#<MyClass:0x007fb6626c7260>"]
class MyClass
def to_json(*args)
{
JSON.create_id => self.class.name,
:a => a,
:b => b
}.to_json(*args)
end
end
puts data.to_json
#=> [{"json_class":"MyClass","a":1,"b":"foobar"}]
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Just as a followup for people finding this later, I added the following monkey-patch to my code:class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
– Tom Bortels
Jun 27 '14 at 17:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24446978%2fhow-can-i-get-rubys-json-to-follow-object-references-like-pry-pp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The .to_json
works in a recursive manner, the default behavior is defined as:
Converts this object to a string (calling to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method to_json was defined for some object.
json
library has added some implementation for some common classes (check the left hand side of this documentation), such as Array
, Range
, DateTime
.
For an array, to_json
first convert all the elements to json object, concat then together, and then add the array mark [
/]
.
For your case, you need to define your customized to_json
method for GuestNicInfo
, NetIpConfigInfo
and NetIpConfigInfoIpAddress
. I don't know your implementation about these three classes, so I wrote a example to demonstrate how to achieve this:
require 'json'
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
end
data = [MyClass.new(1, "foobar")]
puts data.to_json
#=> ["#<MyClass:0x007fb6626c7260>"]
class MyClass
def to_json(*args)
{
JSON.create_id => self.class.name,
:a => a,
:b => b
}.to_json(*args)
end
end
puts data.to_json
#=> [{"json_class":"MyClass","a":1,"b":"foobar"}]
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Just as a followup for people finding this later, I added the following monkey-patch to my code:class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
– Tom Bortels
Jun 27 '14 at 17:49
add a comment |
The .to_json
works in a recursive manner, the default behavior is defined as:
Converts this object to a string (calling to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method to_json was defined for some object.
json
library has added some implementation for some common classes (check the left hand side of this documentation), such as Array
, Range
, DateTime
.
For an array, to_json
first convert all the elements to json object, concat then together, and then add the array mark [
/]
.
For your case, you need to define your customized to_json
method for GuestNicInfo
, NetIpConfigInfo
and NetIpConfigInfoIpAddress
. I don't know your implementation about these three classes, so I wrote a example to demonstrate how to achieve this:
require 'json'
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
end
data = [MyClass.new(1, "foobar")]
puts data.to_json
#=> ["#<MyClass:0x007fb6626c7260>"]
class MyClass
def to_json(*args)
{
JSON.create_id => self.class.name,
:a => a,
:b => b
}.to_json(*args)
end
end
puts data.to_json
#=> [{"json_class":"MyClass","a":1,"b":"foobar"}]
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Just as a followup for people finding this later, I added the following monkey-patch to my code:class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
– Tom Bortels
Jun 27 '14 at 17:49
add a comment |
The .to_json
works in a recursive manner, the default behavior is defined as:
Converts this object to a string (calling to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method to_json was defined for some object.
json
library has added some implementation for some common classes (check the left hand side of this documentation), such as Array
, Range
, DateTime
.
For an array, to_json
first convert all the elements to json object, concat then together, and then add the array mark [
/]
.
For your case, you need to define your customized to_json
method for GuestNicInfo
, NetIpConfigInfo
and NetIpConfigInfoIpAddress
. I don't know your implementation about these three classes, so I wrote a example to demonstrate how to achieve this:
require 'json'
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
end
data = [MyClass.new(1, "foobar")]
puts data.to_json
#=> ["#<MyClass:0x007fb6626c7260>"]
class MyClass
def to_json(*args)
{
JSON.create_id => self.class.name,
:a => a,
:b => b
}.to_json(*args)
end
end
puts data.to_json
#=> [{"json_class":"MyClass","a":1,"b":"foobar"}]
The .to_json
works in a recursive manner, the default behavior is defined as:
Converts this object to a string (calling to_s), converts it to a JSON string, and returns the result. This is a fallback, if no special method to_json was defined for some object.
json
library has added some implementation for some common classes (check the left hand side of this documentation), such as Array
, Range
, DateTime
.
For an array, to_json
first convert all the elements to json object, concat then together, and then add the array mark [
/]
.
For your case, you need to define your customized to_json
method for GuestNicInfo
, NetIpConfigInfo
and NetIpConfigInfoIpAddress
. I don't know your implementation about these three classes, so I wrote a example to demonstrate how to achieve this:
require 'json'
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
end
data = [MyClass.new(1, "foobar")]
puts data.to_json
#=> ["#<MyClass:0x007fb6626c7260>"]
class MyClass
def to_json(*args)
{
JSON.create_id => self.class.name,
:a => a,
:b => b
}.to_json(*args)
end
end
puts data.to_json
#=> [{"json_class":"MyClass","a":1,"b":"foobar"}]
answered Jun 27 '14 at 9:32
Arie Xiao
11.2k21923
11.2k21923
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Just as a followup for people finding this later, I added the following monkey-patch to my code:class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
– Tom Bortels
Jun 27 '14 at 17:49
add a comment |
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Just as a followup for people finding this later, I added the following monkey-patch to my code:class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.
– Tom Bortels
Jun 27 '14 at 17:49
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Good enough - I had hoped there was a magic flag or such that would let it handle this in the same manner as pp, but I gather pp has the same sort of requirements on objects (ie. they need a .pp or such of their own). This is enough that I can add this to the library and submit a patch to the upstream...
– Tom Bortels
Jun 27 '14 at 16:23
Just as a followup for people finding this later, I added the following monkey-patch to my code:
class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.– Tom Bortels
Jun 27 '14 at 17:49
Just as a followup for people finding this later, I added the following monkey-patch to my code:
class RbVmomi::BasicTypes::DataObject def to_json(*args) h = self.props m = h.merge({ JSON.create_id => self.class.name }) m.to_json(*args) end end
and now my to_json recurses down nicely. I'll see about submitting this (or the def, really) to the project.– Tom Bortels
Jun 27 '14 at 17:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24446978%2fhow-can-i-get-rubys-json-to-follow-object-references-like-pry-pp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown