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