org.kohsuke.stapler.StaplerRequest.bindJSON()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(141)

本文整理了Java中org.kohsuke.stapler.StaplerRequest.bindJSON()方法的一些代码示例,展示了StaplerRequest.bindJSON()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StaplerRequest.bindJSON()方法的具体详情如下:
包路径:org.kohsuke.stapler.StaplerRequest
类名称:StaplerRequest
方法名:bindJSON

StaplerRequest.bindJSON介绍

[英]Data-bind from a JSONObject to the given target type, by using introspection or constructor parameters injection.

For example, if you have a constructor that looks like the following:

class Foo { 
@ 
DataBoundConstructorpublic Foo(Integer x, String y, boolean z, Bar bar) { ... } 
} 
class Bar { 
@ 
DataBoundConstructorpublic Bar(int x, int y) {} 
}

... and if JSONObject looks like

{ y:"text", z:true, bar:{x:1,y:2}}

then, this method returns

new Foo(null,"text",true,new Bar(1,2))

Sub-typing

In the above example, a new instance of Bar was created, but you can also create a subtype of Bar by having the '$class' property in JSON like this:

class BarEx extends Bar { 
@ 
DataBoundConstructorpublic BarEx(int a, int b, int c) {} 
} 
{ y:"text", z:true, bar: { $class:"p.k.g.BarEx", a:1, b:2, c:3 } }

The type that shows up in the constructor (Bar in this case) can be an interface or an abstract class.
[中]通过使用内省或构造函数参数注入,将数据从JSONObject绑定到给定的目标类型。
例如,如果您有一个如下所示的构造函数:

class Foo { 
@ 
DataBoundConstructorpublic Foo(Integer x, String y, boolean z, Bar bar) { ... } 
} 
class Bar { 
@ 
DataBoundConstructorpublic Bar(int x, int y) {} 
}

... 如果JSONObject看起来像

{ y:"text", z:true, bar:{x:1,y:2}}

,那么这个方法返回

new Foo(null,"text",true,new Bar(1,2))

####子类型
在上面的示例中,创建了一个新的Bar实例,但您也可以通过在JSON中使用“$class”属性来创建Bar的子类型,如下所示:

class BarEx extends Bar { 
@ 
DataBoundConstructorpublic BarEx(int a, int b, int c) {} 
} 
{ y:"text", z:true, bar: { $class:"p.k.g.BarEx", a:1, b:2, c:3 } }

构造函数中显示的类型(本例中为Bar)可以是接口或抽象类。

代码示例

代码示例来源:origin: jenkinsci/jenkins

@Override
public UserProperty reconfigure(StaplerRequest req, JSONObject form) throws FormException {
  req.bindJSON(this, form);
  return this;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * By default, calls {@link StaplerRequest#bindJSON(Object, JSONObject)},
 * appropriate when your implementation has getters and setters for all fields.
 * <p>{@inheritDoc}
 */
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
  req.bindJSON(this, json);
  return true;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Stick to the same object since there's no UI for this.
 */
@Override
public UserProperty reconfigure(StaplerRequest req, JSONObject form) throws FormException {
  req.bindJSON(this, form);
  return this;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public Publisher newInstance(StaplerRequest req, JSONObject formData) {
  return req.bindJSON(Fingerprinter.class, formData);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public ArtifactArchiver newInstance(StaplerRequest req, JSONObject formData) throws FormException {
  return req.bindJSON(ArtifactArchiver.class,formData);
}

代码示例来源:origin: jenkinsci/jenkins

/**
   * Creates an instance of {@link RepositoryBrowser} from a form submission.
   *
   * @since 1.227
   */
  public static <T extends RepositoryBrowser>
  T createInstance(Class<T> type, StaplerRequest req, JSONObject parent, String fieldName) throws FormException {
    JSONObject o = (JSONObject)parent.get(fieldName);
    if(o==null) return null;

    return req.bindJSON(type,o);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public boolean configure(StaplerRequest req, JSONObject data) throws FormException {
  req.bindJSON(this, data);
  return super.configure(req, data);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public Builder newInstance(StaplerRequest req, JSONObject formData) throws FormException {
    if (req == null) {
      // This state is prohibited according to the Javadoc of the super method.
      throw new FormException("Maven Build Step new instance method is called for null Stapler request. "
          + "Such call is prohibited.", "req");
    }
    return req.bindJSON(Maven.class,formData);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public DefaultCrumbIssuer newInstance(StaplerRequest req, JSONObject formData) throws FormException {
    if (req == null) {
      // This state is prohibited according to the Javadoc of the super method.
      throw new FormException("DefaultCrumbIssuer new instance method is called for null Stapler request. "
          + "Such call is prohibited.", "req");
    }
    return req.bindJSON(DefaultCrumbIssuer.class, formData);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
  RunParameterValue value = req.bindJSON(RunParameterValue.class, jo);
  value.setDescription(getDescription());
  return value;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
  BooleanParameterValue value = req.bindJSON(BooleanParameterValue.class, jo);
  value.setDescription(getDescription());
  return value;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
  TextParameterValue value = req.bindJSON(TextParameterValue.class, jo);
  value.setDescription(getDescription());
  return value;
}

代码示例来源:origin: jenkinsci/jenkins

public static GlobalSettingsProvider parseSettingsProvider(StaplerRequest req) throws Descriptor.FormException, ServletException {
  JSONObject settings = req.getSubmittedForm().getJSONObject("globalSettings");
  if (settings == null) {
    return new DefaultGlobalSettingsProvider();
  }
  return req.bindJSON(GlobalSettingsProvider.class, settings);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
  StringParameterValue value = req.bindJSON(StringParameterValue.class, jo);
  value.setDescription(getDescription());
  return checkValue(value);
}

代码示例来源:origin: jenkinsci/jenkins

public static SettingsProvider parseSettingsProvider(StaplerRequest req) throws Descriptor.FormException, ServletException {
  JSONObject settings = req.getSubmittedForm().getJSONObject("settings");
  if (settings == null) {
    return new DefaultSettingsProvider();
  }
  return req.bindJSON(SettingsProvider.class, settings);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
  StringParameterValue value = req.bindJSON(StringParameterValue.class, jo);
  if (isTrim() && value!=null) {
    value.doTrim();
  }
  value.setDescription(getDescription());
  return value;
}

代码示例来源:origin: jenkinsci/jenkins

public FileParameterValue createValue(StaplerRequest req, JSONObject jo) {
  FileParameterValue p = req.bindJSON(FileParameterValue.class, jo);
  p.setLocation(getName());
  p.setDescription(getDescription());
  return p;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public PasswordParameterValue createValue(StaplerRequest req, JSONObject jo) {
  PasswordParameterValue value = req.bindJSON(PasswordParameterValue.class, jo);
  if (value.getValue().getPlainText().equals(DEFAULT_VALUE)) {
    value = new PasswordParameterValue(getName(), getDefaultValue());
  }
  value.setDescription(getDescription());
  return value;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
    // for compatibility reasons, the actual value is stored in Jenkins
    Jenkins j = Jenkins.get();
    if (json.has("viewsTabBar")) {
      j.setViewsTabBar(req.bindJSON(ViewsTabBar.class,json.getJSONObject("viewsTabBar")));
    } else {
      j.setViewsTabBar(new DefaultViewsTabBar());
    }
    return true;
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
    // for compatibility reasons, the actual value is stored in Jenkins
    Jenkins j = Jenkins.get();
    if (json.has("myViewsTabBar")) {
      j.setMyViewsTabBar(req.bindJSON(MyViewsTabBar.class,json.getJSONObject("myViewsTabBar")));
    } else {
      j.setMyViewsTabBar(new DefaultMyViewsTabBar());
    }
    return true;
  }
}

相关文章

微信公众号

最新文章

更多

StaplerRequest类方法