LightJason - AgentSpeak(L++)
CJsonObject.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin.web.rest;
25 
31 
32 import javax.annotation.Nonnull;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Map;
36 
37 
48 public final class CJsonObject extends IBaseRest
49 {
53  private static final long serialVersionUID = -3741382638836440374L;
54 
58  public CJsonObject()
59  {
60  super( 3 );
61  }
62 
63  @Nonnull
64  @Override
65  @SuppressWarnings( "unchecked" )
66  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
67  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
68  )
69  {
70  try
71  {
72  final Map<String, ?> l_data = IBaseRest.json(
73  p_argument.get( 0 ).<String>raw(),
74  Map.class
75  );
76 
77  p_return.add(
78  p_argument.size() == 2
79  ? CLiteral.from( p_argument.get( p_argument.size() - 1 ).<String>raw(), flatterm( l_data ) )
81  p_argument.stream().skip( 1 ).map( ITerm::<String>raw ),
82  flatterm( l_data )
83  )
84  );
85 
86  return CFuzzyValue.from( true );
87  }
88  catch ( final IOException l_exception )
89  {
90  return CFuzzyValue.from( false );
91  }
92  }
93 
94 }
static Stream< ITerm > flatterm( @Nullable final Object p_object)
converts an object into a term stream
Definition: IBaseWeb.java:178
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
static ITerm baseliteral( @Nonnull final Stream< String > p_functor, @Nonnull final Stream< ITerm > p_values)
creates a literal structure from a stream of string elements, the string stream will be build in a tr...
Definition: IBaseWeb.java:159
execution context with local data
Definition: IContext.java:42
result for an immutable fuzzy value
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
base class to read data from the restful service
Definition: IBaseRest.java:42
static< T > T json( @Nonnull final String p_url, @Nonnull final Class< T > p_class)
reads a json structure from an url
Definition: IBaseRest.java:83
action for calling a restful webservice with a JSON object.