| 1 | package UR::Object::View::Default::Json; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | require UR; | ||
| 6 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 7 | |||
| 8 | use JSON; | ||
| 9 | |||
| 10 | class UR::Object::View::Default::Json { | ||
| 11 | is => 'UR::Object::View::Default::Text', | ||
| 12 | has_constant => [ | ||
| 13 | toolkit => { value => 'json' }, | ||
| 14 | ], | ||
| 15 | has_optional => [ | ||
| 16 | encode_options => { is => 'ARRAY', default_value => ['ascii', 'pretty', 'allow_nonref', 'canonical'], doc => 'Options to enable on the JSON object; see the documentation for the JSON Perl module' }, | ||
| 17 | ], | ||
| 18 | }; | ||
| 19 | |||
| 20 | my $json; | ||
| 21 | sub _json { | ||
| 22 | my ($self) = @_; | ||
| 23 | return $json if defined $json; | ||
| 24 | |||
| 25 | $json = JSON->new; | ||
| 26 | foreach my $opt ( @{ $self->encode_options } ) { | ||
| 27 | eval { $json = $json->$opt; }; | ||
| 28 | if ($@) { | ||
| 29 | Carp::croak("Can't initialize JSON object for encoding. Calling method $opt from encode_options died: $@"); | ||
| 30 | } | ||
| 31 | if (!$json) { | ||
| 32 | Carp::croak("Can't initialize JSON object for encoding. Calling method $opt from encode_options returned false"); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | return $json; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _generate_content { | ||
| 39 | my $self = shift; | ||
| 40 | return $self->_json->encode($self->_jsobj); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _jsobj { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my $subject = $self->subject(); | ||
| 47 | return '' unless $subject; | ||
| 48 | |||
| 49 | my %jsobj = (); | ||
| 50 | |||
| 51 | for my $aspect ($self->aspects) { | ||
| 52 | my $val = $self->_generate_content_for_aspect($aspect); | ||
| 53 | $jsobj{$aspect->name} = $val if defined $val; | ||
| 54 | } | ||
| 55 | |||
| 56 | return \%jsobj; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _generate_content_for_aspect { | ||
| 60 | my $self = shift; | ||
| 61 | my $aspect = shift; | ||
| 62 | |||
| 63 | my $subject = $self->subject; | ||
| 64 | my $aspect_name = $aspect->name; | ||
| 65 | |||
| 66 | my $aspect_meta = $self->subject_class_name->__meta__->property($aspect_name); | ||
| 67 | #warn $aspect_name if ref($subject) =~ /Set/; | ||
| 68 | |||
| 69 | my @value; | ||
| 70 | eval { | ||
| 71 | @value = $subject->$aspect_name; | ||
| 72 | }; | ||
| 73 | if ($@) { | ||
| 74 | warn $@; | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Always look for a delegate view. | ||
| 79 | # This means we replace the value(s) with their | ||
| 80 | # subordinate widget content. | ||
| 81 | unless ($aspect->delegate_view) { | ||
| 82 | $aspect->generate_delegate_view; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $ref = []; | ||
| 86 | |||
| 87 | if (my $delegate_view = $aspect->delegate_view) { | ||
| 88 | foreach my $value ( @value ) { | ||
| 89 | if (Scalar::Util::blessed($value)) { | ||
| 90 | $delegate_view->subject($value); | ||
| 91 | } else { | ||
| 92 | $delegate_view->subject_id($value); | ||
| 93 | } | ||
| 94 | $delegate_view->_update_view_from_subject(); | ||
| 95 | |||
| 96 | if ($delegate_view->can('_jsobj')) { | ||
| 97 | push @$ref, $delegate_view->_jsobj; | ||
| 98 | } else { | ||
| 99 | my $delegate_text = $delegate_view->content(); | ||
| 100 | push @$ref, $delegate_text; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | for my $value (@value) { | ||
| 106 | if (ref($value)) { | ||
| 107 | push @$ref, 'ref'; #TODO(ec) make this render references | ||
| 108 | } else { | ||
| 109 | push @$ref, $value; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | if ($aspect_meta && $aspect_meta->is_many) { | ||
| 115 | return $ref; | ||
| 116 | } else { | ||
| 117 | return shift @$ref; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | # Do not return any aspects by default if we're embedded in another view | ||
| 122 | # The creator of the view will have to specify them manually | ||
| 123 | sub _resolve_default_aspects { | ||
| 124 | my $self = shift; | ||
| 125 | unless ($self->parent_view) { | ||
| 126 | return $self->SUPER::_resolve_default_aspects; | ||
| 127 | } | ||
| 128 | return ('id'); | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; |